Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 794
Doug Smith Avatar asked Oct 06 '13 18:10

Doug Smith


2 Answers

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];
        }
    }
}
like image 175
Stephen Melvin Avatar answered Nov 17 '22 13:11

Stephen Melvin


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.

like image 45
Matt Tang Avatar answered Nov 17 '22 13:11

Matt Tang