UIButton only shows tintColor when pressed and not in normal state

Here is my code. It creates a white button that turns purple when pressed. I want a purple button.

    if (!backButton) {
    backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];
    backButton.titleLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:17];
    CGSize size = [aboutButton.titleLabel.text sizeWithFont:[UIFont fontWithName:@"Helvetica Neue" size:23]];
    backButton.frame = CGRectMake(screenWidth/2 - size.width/2, screenHeight-size.height*4, size.width, size.height);
    backButton.tintColor = [UIColor colorWithRed:123/255.0 green:47/255.0 blue:85/255.0 alpha:1]; //This is the color I want the button to be in its normal state.
    [backButton addTarget:self action:NSSelectorFromString(@"displaySettingsScreen") forControlEvents:UIControlEventTouchUpInside];
}
[self.view addSubview:backButton];

      

What am I missing?

+3


source to share


2 answers


Unfortunately, the tintColor property does not work for RoundedRect button types.

See here: Why is my UIButton.tintColor not working?

To work around this, I use a single-segment UISementedControl instead of the UIButton I need, colored.



This answer describes the technique: fooobar.com/questions/727194 / ...

Another alternative would be to use a purple image for the button, although this would take a little more work.

+2


source


Buttons with rounded buttons are hard to change, so use a custom button instead and set the background color:

if (!backButton) {
        backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setTitle:@"Back" forState:UIControlStateNormal];
        backButton.titleLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:17];
        CGSize size = [aboutButton.titleLabel.text sizeWithFont:[UIFont fontWithName:@"Helvetica Neue" size:23]];
        backButton.frame = CGRectMake(screenWidth/2 - size.width/2, screenHeight-size.height*4, size.width, size.height);
        backButton.layer.cornerRadius = 6.0f;
        backButton.backgroundColor = [UIColor colorWithRed:123/255.0 green:47/255.0 blue:85/255.0 alpha:1]; //This is the color I want the button to be in its normal state.
        [backButton addTarget:self action:NSSelectorFromString(@"displaySettingsScreen") forControlEvents:UIControlEventTouchUpInside];
    }
    [self.view addSubview:backButton];

      



You need to add the QuartzCore framework and import it (for the layer properties to work).

+1


source







All Articles