【问题标题】:How to set UIViewTintAdjustmentModeDimmed on a UIButton如何在 UIButton 上设置 UIViewTintAdjustmentModeDimmed
【发布时间】:2017-03-09 19:04:07
【问题描述】:

我正在尝试使按钮显示为已禁用,但实际上并未禁用它。

我想要灰色的颜色。

按钮是在界面生成器中输入Custom。 它有一张背景图片。

我不想弄乱图像状态,我只想随时将其更改为灰色,就像设置 myButton.enabled = NO 将其设置为灰色一样。

所以我尝试在 viewDidLoad 中设置someButton.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed,甚至进一步进入应用程序,它仍然呈现典型的亮蓝色。

按钮上是否还有其他属性我需要设置/更改才能自己控制 tintAdjustmentMode?​​p>

我什至尝试了自己的按钮,使用以下代码,但也没有用。

@implementation TintableUIButton

    -(id)init
        {
            self = [super init];
            if (self) {
                self.tintColor = [UIColor grayColor];
                self.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
            }
            return self;
        }

@end

【问题讨论】:

  • 为什么不直接将 UIButton 颜色属性设置为您正在寻找的灰色颜色。 .tintColor / .titleColor 等
  • @Daniel ~ 那也不行。 myButton.tintColor = [UIColor grayColor] 什么都不做。色调在整个层次结构中得到了强有力的强制执行,我正试图获得对它的一盎司控制。
  • 自定义按钮不服从色调。您必须自己重新着色图像。这是我为按钮标题的文本执行此操作的示例。 github.com/mattneub/Programming-iOS-Book-Examples/blob/master/…
  • @matt。是的,看起来我唯一的选择是myButton setImage:forState:,只需交换预着色的图像。哦,好吧。
  • 正是我想的那种事情。

标签: ios objective-c uibutton


【解决方案1】:

试试这个,

您可以创建一个自定义类并添加如下方法:

class customBtnClass: UIButton {
     func fakeIsEnable(enable: Bool){
        if (enable){
            self.tintColor = .blue // your default tint
        }else{
            self.tintColor = .gray
        }
    }
}

Drag a simple button and set the custom class

创建一个 IBOutlet 并调用方法

@IBOutlet weak var btnTest: customBtnClass!
override func viewDidLoad() {
    super.viewDidLoad()
    btnTest.fakeIsEnable(enable: false)
}

目标c:

-(void) fakeIsEnable:(BOOL) enable{
    if (enable){
        self.tintColor = [UIColor blueColor]; // your default tint
    }else{
        self.tintColor = [UIColor grayColor];
    }
}

在您的 ViewController 中(不要忘记 #import "CustomBtnClass.h")

@interface ViewController ()
@property (weak, nonatomic) IBOutlet CustomBtnClass *yourButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.yourButton fakeIsEnable:NO];
}

问候

【讨论】:

  • 更新了我上面的答案,这不起作用。 :( 我只是根据需要将图像换成重新着色的图像。我希望在使用 .enabled = NO 时使用已经内置的着色,但我尝试过的方式似乎无法使用该功能.
【解决方案2】:

您可以在点击事件上添加一行代码 最初在 viewdidload() 中保持按钮 switchBool 为 false

-(IBAction) btn_Action_Inside:(id)sender
{

    if (!switchBool) {

        [switchMap setBackgroundImage:[UIImage imageNamed:@"NormalImage"] forState:UIControlStateNormal];
        switchBool = true;
    }
    else if(switchBool)
    {

        [switchMap setBackgroundImage:[UIImage imageNamed:@"GrayedcolorImage"] forState:UIControlStateNormal];
        switchBool = false;
    }

}

或者你可以选择

-(IBAction)btn_Action_Inside:(id)sender
    {

        if (!switchBool) {

                self.yourButton.alpha = 1.0f;
                switchBool = true;
        }
        else if(switchBool)
        {

                self.yourButton.alpha = 0.5f;
                switchBool = false;
        }

    }

或者你可以选择这个

-(IBAction)btn_Action_Inside:(id)sender
    {

        if (!switchBool) {


                [self.yourButton setTintColor:[UIColor redColor]];
                switchBool = true;
        }
        else if(switchBool)
        {

                [self.yourButton setTintColor:[UIColor grayColor]];
                switchBool = false;
        }

    }

【讨论】:

  • 是的,根据我原始帖子中的评论,我正在做setImage:forState:setTintColor: 不像我和马特指出的那样工作。
猜你喜欢
  • 1970-01-01
  • 2012-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多