10

I use green as the "action color" throughout my app, and I want the options in my UIActionSheets to be green as well, for consistency. How can I change the colour of the UIActionSheet options to green from blue?

Doug Smith
  • 29,668
  • 57
  • 204
  • 388

3 Answers3

28

Utilize the willPresentActionSheet delegate method of UIActionSheet to change the action sheet button color.

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    for (UIView *subview in actionSheet.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            button.titleLabel.textColor = [UIColor greenColor];
        }
    }
}
Stephen Melvin
  • 3,696
  • 2
  • 27
  • 40
  • 3
    This answer is better. Uses a delegate method not to mention more concise. Also the above answer doesn't change the cancel button's color. – LazerLex Oct 10 '13 at 10:08
  • For some reason, if you also want to change the font, you have to do that before changing the color, or it won't work – Breno Gazzola Oct 29 '13 at 23:26
  • @BrenoGazzola: What happens? – Stephen Melvin Oct 30 '13 at 15:05
  • When doing `button.titleLabel.textColor = MY_COLOR;button.titleLabel.font = MY_FONT;` the options were changed to the new font but remained blue and red. Simply changing the order to `button.titleLabel.font = MY_FONT;button.titleLabel.textColor = MY_COLOR;` fixed the problem and I got both the new font and new colors. – Breno Gazzola Oct 31 '13 at 21:06
  • That is strange. Which version of iOS are you running and what device/simulator are you using to run the code? If I can replicate this tonight, I'll submit a bug ticket to apple. – Stephen Melvin Nov 01 '13 at 14:22
  • don't forget to set the button's color for selected states as well! http://stackoverflow.com/questions/7806840/how-to-set-the-buttons-label-text-color-for-state-uicontrolstatehighlighted Regular buttons will use UIControlStateHighlighted when tapped, but the Cancel button will use UIControlStateSelected – Rembrandt Q. Einstein Apr 21 '14 at 21:16
18

You could do something like this:

// Your code to instantiate the UIActionSheet
UIActionSheet *actionSheet = [[UIActionSheet alloc] init];
// Configure actionSheet

// Iterate through the sub views of the action sheet
for (id actionSheetSubview in actionSheet.subviews) {
    // Change the font color if the sub view is a UIButton
    if ([actionSheetSubview isKindOfClass:[UIButton class]]) {
        UIButton *button = (UIButton *)actionSheetSubview;
        [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];

    }
}

If you're going to reuse this a lot, I'd subclass UIActionSheet and use this code.

Matt Tang
  • 1,297
  • 11
  • 17
0

You can change easily the application's tint color using this short function on your AppDelegate didFinishLaunchingWithOptions method.

[[UIView appearance] setTintColor:[UIColor redColor]];

Hope it will help you :)

tryp
  • 1,120
  • 20
  • 26