UIButton颜色问题
如何更改UIButton上文本的颜色。 这是我目前的代码:
UIButton *b1 = [[UIButton alloc] init]; b1.frame = CGRectMake(280,395,30,30); [[b1 layer] setCornerRadius:8.0f]; [[b1 layer] setMasksToBounds:YES]; [[b1 layer] setBorderWidth:1.0f]; [[b1 layer] setBackgroundColor:[botCol CGColor]]; b1.titleLabel.font = [UIFont boldSystemFontOfSize:24]; [b1 setTitleColor:[UIColor redColor] forState:UIControlEventAllEvents]; [b1 addTarget:self action:@selector(NextButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; [b1 setTitle:@">" forState:UIControlStateNormal]; [self.view addSubview:b1];
这是它的样子(忽略背景颜色和东西):
现在,我怎么能让箭头变成红色? 如您所见,我已经有以下内容:
[b1 setTitleColor:[UIColor redColor] forState:UIControlEventAllEvents];
但它不起作用。
尝试设置各个事件,例如:
[b1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
原始代码无法工作的原因是因为您传入的是UIControlEvents
参数而不是UIControlState
参数。
[b1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
这将设置正常状态的颜色,除非您为其他状态设置颜色,否则它将在所有状态中保持不变。 要更改其他状态的颜色,只需调用与其他状态相同的方法( UIControlStateNormal
, UIControlStateHighlighted
, UIControlStateDisabled
, UIControlStateSelected
):
[b1 setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];