Why is my UIButton.tintColor not working?

后端 未结 13 1069
长情又很酷
长情又很酷 2020-12-03 04:15

My build target is set for IOS5 which is where I understand UIButton.tintColor was introduced...

I have this in my viewDidLoad for the View Controller

相关标签:
13条回答
  • 2020-12-03 04:43

    As stated in other answers tint does not work for Custom Button types. Make sure you explicitly declare the button type. Do not just use [UIButton alloc] init]

    This will work:

    UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [mybutton setImage:[UIImage imageNamed:@"myImage"] forState:UIControlStateNormal];
    mybutton.tintColor = [ODTheme blueTintColor];
    
    0 讨论(0)
  • 2020-12-03 04:48

    According to the documentation:

    This property is not valid for all button types.

    You need to switch your buttonType to another one of these. (Unfortunately, the documentation does not specify which specific button types support this property.)

    0 讨论(0)
  • 2020-12-03 04:48

    Make sure your button "type" is set to System. If it is set to Custom it could be the reason the color of your button isn't changing. This is what happened to me and changing the type to System fixed it.

    0 讨论(0)
  • 2020-12-03 04:48

    Swift 4 :

    yourButton.setTitleColor(UIColor.white, for: .normal)
    
    0 讨论(0)
  • 2020-12-03 04:50

    I found 3 things must be followed.

    1. Render image as Default

    Go to Images.xcassets > Your Image > Show the Attributes inspector > Render As > Default

    2. Make sure your button type is System

    3. Now change button's tintColor.

    Done.

    0 讨论(0)
  • 2020-12-03 04:51

    Simply use:

    [yourButton setBackgroundImage:[yourButton.currentBackgroundImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal]; 
    [yourButton setTintColor:[UIColor blackColor]];
    
    0 讨论(0)
提交回复
热议问题