为什么下面的代码起作用...
[signInBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[signInBtn setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];
虽然不是吗?
[signInBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted|UIControlStateDisabled];
我知道这是一个老问题,但是这些答案不正确。
当分别设置每一个时,您说的是state
属性应该是UIControlStateHighlighted
或UIControlStateDisabled
,但不能同时是两个]
[按位或将它们一起表示时,必须同时在state
属性中进行设置。含义在UIControlStateHighlighted
属性中设置了UIControlStateDisabled
和state
。
下面的示例代码完美地说明了我的观点。如果您不同意自己运行它。
[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
因为setTitleColor:forState:方法只能接受已知的UIControlState,并且您将两个UIControlState值进行或运算。
可能是一个错误。尝试更改具有UIControlStateHighlighted
和UIControlStateDisabled
之类的意外值的位掩码,这会使所有状态颜色相同。