【问题标题】:UIButton won't gray outUIButton 不会变灰
【发布时间】:2011-09-12 15:30:55
【问题描述】:

UIButton 不应该在 enabled=NO 时变成灰色/灰色吗?

我在黑色背景上有一个简单的 UIButton(没有自定义图像,没有自定义任何内容,只是用 IB 拖动它并更改了大小和标题)。

当我以编程方式将其设置为禁用时,它会保持白色!

现在我正在使用一个愚蠢的小解决方法:隐藏的 blackbg 0,5 alpha UIView on the button that become hidden=NO 当我需要禁用按钮时...但我想正​​确设置按钮...

有什么想法吗?

【问题讨论】:

    标签: iphone ios uibutton


    【解决方案1】:

    只需创建一个如下所示的 UIButton 类别,然后在要使用它的类中导入 #import "UIButton+StateColors.h"

    .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
    

    然后像这样使用它......

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

    我希望这是一个通用的解决方案...

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,因为我设置了背景颜色。

      我删除了背景颜色并将其设置为仅UIControlStateNormal,并且启用/禁用的默认行为开始出现。

      如果您正在设置背景颜色而不是图像,请尝试使用此类别将 UIColor 转换为 UIImage:

      复制自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;
      }
      

      然后使用这个:

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

      将颜色设置为背景。现在,当您启用/禁用时,应该会出现灰色效果。

      【讨论】:

        【解决方案3】:

        我偶然发现了这个问题,Apple 发布了一个新的UIKit User Interface Catalog,用于在 iOS 7 中使用按钮。

        针对您的问题,UIButton Class 现在公开了一个名为 adjustsImageWhenDisabled 的属性,它是“一个布尔值,用于确定在禁用按钮时图像是否发生变化”。

        如果这个adjustsImageWhenDisabled属性设置为“YES,当按钮被禁用时,图像会被绘制得更暗。默认值为YES。”

        【讨论】:

          【解决方案4】:

          还有另一种方法,无需对整个按钮进行 alpha 处理:

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

          然后,每当您将 enabled 属性设置为 NO 时,按钮的文本就会自动变灰。

          【讨论】:

            【解决方案5】:

            没有办法让 UIButton “变灰”。但是你可以使用这个技巧:

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

            所以你的 UIButton 看起来无法使用 ;)

            【讨论】:

            • 是的,由于 UIButton 位于黑色视图之上,因此更改 alpha 会产生与我的黑色 UIView 完全相同的效果,但这是一个更聪明、更简单的解决方案,应该首先想到这一点- 我要换了 ;) 谢谢
            • 简单的好答案。如果你想让它看起来完全像一个禁用的按钮,它实际上是 0.5 alpha。
            猜你喜欢
            • 2011-08-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-10-01
            • 1970-01-01
            相关资源
            最近更新 更多