为什么以下代码有效:
[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