Doug Smith
Doug Smith

Reputation: 29326

In iOS 7, how do I change the color of the options in my UIActionSheet?

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?

Upvotes: 10

Views: 9063

Answers (3)

tryp
tryp

Reputation: 1120

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 :)

Upvotes: 0

Matt Tang
Matt Tang

Reputation: 1297

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.

Upvotes: 18

Stephen Melvin
Stephen Melvin

Reputation: 3785

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];
        }
    }
}

Upvotes: 28

Related Questions