选择时自定义按钮不更改文本颜色

Posted

技术标签:

【中文标题】选择时自定义按钮不更改文本颜色【英文标题】:Custom button not changing text color when selected 【发布时间】:2017-11-30 09:52:09 【问题描述】:

我使用 for 循环创建了 3 个自定义按钮。但是当我选择一个按钮时,文本颜色没有改变。我该怎么做?还有什么要补充的?

这是我的代码

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];

【问题讨论】:

您想在按钮点击时更改文本颜色?? .normal 颜色是什么,如何设置?这是默认颜色。如果这不同,那么上面现在显示的内容有问题,因为上面的代码正确设置了您在点击按钮时应该看到的颜色。 @meaning-matters 正常颜色设置为黑色,点击时我想要按钮颜色,但它仍然是黑色 @ZeenathYousuff 确保您的按钮标题不是属性字符串 不是属性字符串。 【参考方案1】:

我已经尝试了您的带有虚拟文本的单个按钮的代码,正常状态的黑色文本颜色和按钮颜色在点击时突出显示:

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];


    

【讨论】:

是的。我有同样的问题,但希望该颜色保持不变,直到我选择其他按钮 我在那里尝试过作为@JatinRB Korat 的答案。但是一旦设置它就不会在点击其他按钮时设置回未选择的颜色 我只在这里编辑了答案。试试这个。可能你会得到一些语法错误,因为我只在这里编辑了代码。但我希望它能解决你的问题 它崩溃说 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setTitleColor:forState:]: unrecognized selector sent to instance 0x7faeb741c400' 。我尝试将 [navigationView viewWithTag:i*200] 更改为 UIButton *restBtn= (UIButton *)[navigationView viewWithTag:i]; 仍然崩溃 你只传递了'i',因为那个标签不匹配。使用 i*200 那么只有标签会匹配【参考方案2】:
    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];
    

【讨论】:

点击时会改变颜色。但是当我点击其他按钮时,它仍然是相同的颜色,我想将未选择的更改为灰色并选择其他按钮 tapButtonAction 的一些变化,请再次参考

以上是关于选择时自定义按钮不更改文本颜色的主要内容,如果未能解决你的问题,请参考以下文章

当我设置自定义边框或背景颜色时,为什么Firefox在选择箭头按钮上设置背景颜色?

使用 Bootstrap 自定义更改按钮上的悬停颜色

空白时自定义文本输入更改标签

选中时更改按钮文本颜色

基于控件可见性在运行时自定义动态布局

使用文本而不是图标时自定义 Material UI Table 图标属性的字体