UIButton setTitleColor:forState:问题

问题描述 投票:14回答:3

为什么下面的代码起作用...

[signInBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[signInBtn setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];

虽然不是吗?

[signInBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted|UIControlStateDisabled];
iphone objective-c cocoa-touch uikit
3个回答
29
投票

我知道这是一个老问题,但是这些答案不正确。

当分别设置每一个时,您说的是state属性应该是UIControlStateHighlightedUIControlStateDisabled,但不能同时是两个]

[按位或将它们一起表示时,必须同时在state属性中进行设置。含义在UIControlStateHighlighted属性中设置了UIControlStateDisabledstate

下面的示例代码完美地说明了我的观点。如果您不同意自己运行它。

[button setTitle:@"highlighted and selected" forState:UIControlStateHighlighted | UIControlStateSelected];
[button setTitle:@"Highlighted only" forState:UIControlStateHighlighted];
[button setTitle:@"Selected only" forState:UIControlStateSelected];
[button setTitle:@"Normal" forState:UIControlStateNormal];

NSLog(@"Normal title: %@", [[button titleLabel] text]); // prints title: Normal

[button setSelected:YES];

NSLog(@"Selected title: %@", [[button titleLabel] text]); // prints title: Selected only 

[button setSelected:NO];
[button setHighlighted:YES];

NSLog(@"highlighted title: %@", [[button titleLabel] text]); // prints title: Highlighted only

[button setSelected:YES];

NSLog(@"highlighted and selected title: %@", [[button titleLabel] text]); // prints title: highlighted and selected

2
投票

因为setTitleColor:forState:方法只能接受已知的UIControlState,并且您将两个UIControlState值进行或运算。


-1
投票

可能是一个错误。尝试更改具有UIControlStateHighlightedUIControlStateDisabled之类的意外值的位掩码,这会使所有状态颜色相同。

© www.soinside.com 2019 - 2024. All rights reserved.