16

How can I change the color of UIActionSheet button's color?

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Abhinav
  • 37,684
  • 43
  • 191
  • 309

5 Answers5

34

iOS 8 (UIAlertController)

It's super simple to do if you're using a UIAlertController. Simply change the tint color on the view of the UIAlertController.

[alertController.view setTintColor:[UIColor red];

iOS 7 (UIActionSheet)

I successfully change the text color by using this simple method.

- (void) changeTextColorForUIActionSheet:(UIActionSheet*)actionSheet {
    UIColor *tintColor = [UIColor redColor];

    NSArray *actionSheetButtons = actionSheet.subviews;
    for (int i = 0; [actionSheetButtons count] > i; i++) {
        UIView *view = (UIView*)[actionSheetButtons objectAtIndex:i];
        if([view isKindOfClass:[UIButton class]]){
            UIButton *btn = (UIButton*)view;
            [btn setTitleColor:tintColor forState:UIControlStateNormal];

        }
    }
}

Make sure to run this AFTER you call

[actionSheet showInView];

If you call it before [showInView], all buttons but the cancel button will be colored. Hope this helps someone!

Rymnel
  • 4,515
  • 3
  • 27
  • 28
7

I have created child class UICustomActionSheet, which allows customize fonts, colors and images of buttons inside UIActionSheet. It is absolutely safety for appstore, you can find code of this class on next link:

https://github.com/gloomcore/UICustomActionSheet

Enjoy it!

Gloomcore
  • 940
  • 7
  • 11
1

We can use background images to do it. I think it is the easiest way.

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Actionsheet" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
        [actionSheet addButtonWithTitle:@"Button 1"]; //Blue color
        [actionSheet addButtonWithTitle:@"Button 2"];
        [actionSheet addButtonWithTitle:@"Cancel"];
        [actionSheet addButtonWithTitle:nil];
        [actionSheet setCancelButtonIndex:2];
        [actionSheet setDestructiveButtonIndex:1];
        [actionSheet showInView:self.view];
        UIButton *button = [[actionSheet subviews] objectAtIndex:1];
        UIImage *img = [button backgroundImageForState:UIControlStateHighlighted];//[UIImage imageNamed:@"alert_button.png"];
        [button setBackgroundImage:img forState:UIControlStateNormal];
Mr Phuc 87
  • 184
  • 2
  • 7
1

Unfortunately, without using undocumented API's, there is no official way to change the button's color on a UIActionSheet. You may be able to customize this if you subclass the UIActionSheet control.

See this example: http://blog.corywiles.com/customizing-uiactionsheet-buttons

Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
  • Did you ever did this? Any sample code will be really helpful. – Abhinav Nov 22 '10 at 17:47
  • 3
    It's not difficult to change the button color and you can do it without using undocumented API's. – Rymnel Nov 05 '13 at 21:33
  • You should not subclass `UIActionSheet`. From the documentation: `UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy. If you need to present a sheet with more customization than provided by the UIActionSheet API, you can create your own and present it modally with presentViewController:animated:completion:.` – memmons Mar 11 '14 at 20:13
0

You can easily achieve it by using following code

Apple UIActionSheetDelegate protocol documentation

https://developer.apple.com/library/ios/documentation/uikit/reference/UIModalViewDelegate_Protocol/UIActionSheetDelegate/UIActionSheetDelegate.html

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet 
{
    for (UIView *_currentView in actionSheet.subviews) 
    {
        if ([_currentView isKindOfClass:[UIButton class]]) 
        {    
            UIButton *button = (UIButton *)_currentView;
            [button setTitleColor:YOUR_COLOR forState:UIControlStateNormal];
        }
    }
}
Akshit Zaveri
  • 4,166
  • 6
  • 30
  • 59