UIButton won't gray out

前端 未结 5 1404
后悔当初
后悔当初 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

    Simply make a UIButton category like the following and import #import "UIButton+StateColors.h" in the classes where you want to use it.

    .h

    #import <UIKit/UIKit.h>
    
    @interface UIButton (StateColors)
    
    -(void)makeDisabled:(BOOL)flag;
    
    @end
    

    .m

    #import "UIButton+StateColors.h"
    
    #define ENABLED_BUTTON_ALPHA 1
    #define DISABLED_BUTTON_ALPHA 0.3
    
    @implementation UIButton (StateColors)
    
    -(void)makeDisabled:(BOOL)flag {
        self.enabled = !flag;
        self.alpha = flag ? DISABLED_BUTTON_ALPHA : ENABLED_BUTTON_ALPHA;
    }
    
    
    @end
    

    And use it like this...

    [self.emailBtn makeDisabled:NO];
    [self.printBtn makeDisabled:YES];
    

    It's a universal solution I hope...

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-30 21:53

    There is no way to make a UIButton "grayer". But you can use that trick :

    UIButton *myButton;
    myButton.alpha = 0.4;
    myButton.enabled = NO;
    

    So your UIButton looks like unusable ;)

    0 讨论(0)
  • 2020-12-30 22:02

    There is another way without having to alpha the whole button:

    [startButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
    

    Then whenever you set the enabled property to NO, the button's text will automatically gray out.

    0 讨论(0)
  • 2020-12-30 22:04

    I happened upon this question and Apple has published a new UIKit User Interface Catalog for working with Buttons in iOS 7.

    In response to your question, the UIButton Class now exposes a property called adjustsImageWhenDisabled, which is "a Boolean value that determines whether the image changes when the button is disabled."

    If this adjustsImageWhenDisabled property is set to "YES, the image is drawn darker when the button is disabled. The default value is YES."

    0 讨论(0)
提交回复
热议问题