UIButton won't gray out

前端 未结 5 1512
后悔当初
后悔当初 2020-12-30 21:37

Isn\'t UIButton supposed to become grayish/grayer when enabled=NO ?

I have a simple UIButton on a blackbackground (no custom images, no custom nothing, just dragged

5条回答
  •  灰色年华
    2020-12-30 21:47

    I face the same problem because I had set background color.

    I removed the background color and set it for UIControlStateNormal only and the default behaviour for enable/disable started to appear.

    If you are setting background color instead of image try this category for converting UIColor to UIImage:

    copied from here:

    + (UIImage *)imageWithColor:(UIColor *)color
    {
        CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(context, [color CGColor]);
        CGContextFillRect(context, rect);
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return image;
    }
    

    then use this:

    [self.loginButton setBackgroundImage:[UIImage imageWithColor:greenColor] forState:UIControlStateNormal];
    self.loginButton.enabled = NO;
    

    to set the color as background. Now when you enable/disable, the gray effect should appear.

提交回复
热议问题