Abhinav
Abhinav

Reputation: 38142

UIActionSheet button's color

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

Upvotes: 16

Views: 22131

Answers (5)

Rymnel
Rymnel

Reputation: 4515

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!

Upvotes: 34

Akshit Zaveri
Akshit Zaveri

Reputation: 4244

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

Upvotes: 0

Mr Phuc 87
Mr Phuc 87

Reputation: 174

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

Upvotes: 1

Gloomcore
Gloomcore

Reputation: 950

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!

Upvotes: 7

Evan Mulawski
Evan Mulawski

Reputation: 55334

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

Upvotes: 1

Related Questions