Why is my UIButton.tintColor not working?

后端 未结 13 1070
长情又很酷
长情又很酷 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:59

    It tint your highlighted State color. When you tap/click on the UIButton the color specified with tintColor appears as long as you hold the tap/click on the UIButton.

    resetButton.tintColor = [UIColor colorWithRed:0.764 green:1.000 blue:0.000 alpha:1.000];
    

    The button is white in normal state. But if I tap on the button the color turns red, but only then.

    IF you need to change the button so it looks like a red or blue one in the UIControlStateNormal then

    Change the UIButtonType to UIButtonTypeCustom in Interface Builder or programmatically with

     UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
    

    Change the attributes on your own and recreate the rounded corners

    resetButton.backgroundColor = [UIColor redColor];
    resetButton.layer.borderColor = [UIColor blackColor].CGColor;
    resetButton.layer.borderWidth = 0.5f;
    resetButton.layer.cornerRadius = 10.0f;
    
    0 讨论(0)
  • 2020-12-03 04:59

    Simply set your UIButton to type System in Storyboard.

    And then in code just use:

    myButton.tintColor = UIColor.whiteColor()
    
    0 讨论(0)
  • 2020-12-03 04:59

    If your UIButton type is system then and then only tint colour property is work. So first you need to set your button type to the system then apply tint colour for the specific state.

    let btn = UIButton.init(type: .system)
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    0 讨论(0)
  • 2020-12-03 05:04

    Today, I also meet this problem. I use delegate to solve it.

    [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchDown];
    [button addTarget:self action:@selector(buttonPressReset:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
    
    -(void)buttonPress:(id)sender{
    UIButton* button = (UIButton*)sender;
    [button setBackgroundColor:[UIColor greenColor]];
    NSLog(@"buttonPressed");
    }
    
    -(void)buttonPressReset:(id)sender{
    UIButton* button = (UIButton*)sender;
    [button setBackgroundColor:[UIColor redColor]];
    NSLog(@"buttonPressReset");
    }
    
    0 讨论(0)
  • 2020-12-03 05:04

    Should try this method:

    - (void)setTitleColor:(UIColor *)color
                 forState:(UIControlState)state
    
    0 讨论(0)
  • 2020-12-03 05:04

    To change the color of the Button text you can use:

    resetButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
    

    Or OBJ-C:

    - (void)setTitleColor:(UIColor *)color
             forState:(UIControlState)state
    

    https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/#//apple_ref/occ/instm/UIButton/setTitleColor:forState:

    0 讨论(0)
提交回复
热议问题