我已经尝试了带有虚拟文本的单个按钮的代码,正常状态的黑色文本颜色和按钮颜色在点击时突出显示:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(20, 40,50 , 35);
// button.frame = CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT);
[button setTitle:@"Dummy" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
下面的行不会长时间更改按钮文本颜色,它会在点击后几秒钟内更改按钮的文本颜色
[按钮 setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateHighlighted];
尝试这个:
NSInteger numControllers = [viewControllerArray count];
for (int i = 0; i<numControllers; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER,(self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT);
button.tag=i+200;
[button setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1]];
[button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[navigationView addSubview:button];
}
- (IBAction)tapButtonAction:(id)sender
{
UIButton *btn = (UIButton*)sender;
[btn setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];
for (int i = 0; i<numControllers; i++) {
UIButton *restBtn=[navigationView viewWithTag:i+200];
if(restbtn!= btn)
{
[restBtn setBackgroundColor:[UIColor grayColor] forState: UIControlStateNormal];
[restBtn setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];
}
}
}