UIButton color issues

前端 未结 2 1919
伪装坚强ぢ
伪装坚强ぢ 2020-12-21 07:31

How can I change the color of the text on my UIButton. Here is my current code:

    UIButton *b1 = [[UIButton alloc] init];
    b1.frame = CGRectMake(280,395         


        
相关标签:
2条回答
  • 2020-12-21 08:16

    The reason your original code wasn't working is because you were passing in a UIControlEvents parameter instead of a UIControlState parameter.

    [b1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    

    That will set the color for the normal state, and, unless you set colors for other states, it will persist across all states. To change the color for the other states, just call the same method with other states (UIControlStateNormal, UIControlStateHighlighted, UIControlStateDisabled, UIControlStateSelected):

    [b1 setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    
    0 讨论(0)
  • 2020-12-21 08:30

    Try setting the individual events, such as:

    [b1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    0 讨论(0)
提交回复
热议问题