选择时自定义按钮不更改文本颜色
Custom button not changing text color when selected
我使用 for 循环创建了 3 个自定义按钮。但是当我 select 一个按钮时,文本颜色不是 changing.How 我会这样做吗?我还需要补充什么吗?
这是我的代码
buttonText = [[NSArray alloc]initWithObjects: @"Slambook",@"Initiated",@"Collaborated",nil];
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 setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateSelected];
[button setTitle:[buttonText objectAtIndex:i] forState:UIControlStateNormal];
[button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[navigationView addSubview:button];
}
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 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;
if ([btn isSelected]) {
[btn setSelected:NO];
[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];
}
else
{
[btn setSelected:YES];
[btn setBackgroundColor:[UIColor colorWithRed:204.0/255.0 green:24.0/255.0 blue:204.0/255.0 alpha:1] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
}
使用此代码更改您的 for 循环
int x = 0;
for (int i = 0; i < [viewControllerArray count]; i++){
//int y=2;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x+2, 0, 40, 36)];
id myArrayElement=[viewControllerArray objectAtIndex:i];
[button setTitle:[NSString stringWithFormat:@"%@",myArrayElement] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackcolor]];
button.titleLabel.font = [UIFont systemFontOfSize:15];
x += button.frame.size.width;
[button setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
[button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
我已经试过你的单按钮代码,带有虚拟文本,正常状态的黑色文本颜色和按钮颜色在点击时突出显示:
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];
}
}
}
我使用 for 循环创建了 3 个自定义按钮。但是当我 select 一个按钮时,文本颜色不是 changing.How 我会这样做吗?我还需要补充什么吗?
这是我的代码
buttonText = [[NSArray alloc]initWithObjects: @"Slambook",@"Initiated",@"Collaborated",nil];
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 setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateSelected];
[button setTitle:[buttonText objectAtIndex:i] forState:UIControlStateNormal];
[button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[navigationView addSubview:button];
}
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 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;
if ([btn isSelected]) {
[btn setSelected:NO];
[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];
}
else
{
[btn setSelected:YES];
[btn setBackgroundColor:[UIColor colorWithRed:204.0/255.0 green:24.0/255.0 blue:204.0/255.0 alpha:1] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
}
使用此代码更改您的 for 循环
int x = 0;
for (int i = 0; i < [viewControllerArray count]; i++){
//int y=2;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x+2, 0, 40, 36)];
id myArrayElement=[viewControllerArray objectAtIndex:i];
[button setTitle:[NSString stringWithFormat:@"%@",myArrayElement] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackcolor]];
button.titleLabel.font = [UIFont systemFontOfSize:15];
x += button.frame.size.width;
[button setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
[button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
我已经试过你的单按钮代码,带有虚拟文本,正常状态的黑色文本颜色和按钮颜色在点击时突出显示:
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];
}
}
}