tintColor of UIActionSheet (or UIAlertView) (iOS 7+)
Asked Answered
F

3

6

Is it possible to have buttons in UIActionSheet in iOS 7's tintColor color? I mean if my app is in brand tintColor, for example red, I don't want blue buttons in action sheet. The same with UIAlertView.

Fiat answered 24/10, 2013 at 19:59 Comment(4)
#16713807Ergograph
John Riselvato is right in linking to the answer, "No, neither UIAlertView or UIActionSheet are designed by Apple to be subclassed." For custom appearance, you need to roll your own.Childbed
https://mcmap.net/q/450727/-uiactionsheet-button-39-s-color/…Excruciating
github.com/gloomcore/UICustomActionSheetSielen
B
5

I want to stress that this violates Apple's rules, but this works:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    [actionSheet.subviews enumerateObjectsUsingBlock:^(UIView *subview, NSUInteger idx, BOOL *stop) {
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            button.titleLabel.textColor = [UIColor greenColor];
            NSString *buttonText = button.titleLabel.text;
            if ([buttonText isEqualToString:NSLocalizedString(@"Cancel", nil)]) {
               [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
            }
        }
    }];
}

(conform to UIActionSheetDelegate)

Not tried UIAlertView yet.

Berserker answered 11/2, 2014 at 0:11 Comment(1)
You should be using the following method to update the button title color: [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];Merritt
K
1

It is possible. Here is a quick implementation for iOS7:

@interface LNActionSheet : UIActionSheet
{
    NSString* _destructiveButtonTitle;
    UIColor* _customtintColor;
}

@end

@implementation LNActionSheet

- (id)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
{
    self = [super initWithTitle:title delegate:delegate cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:nil];

    if(self)
    {
        va_list list;
        va_start(list, otherButtonTitles);

        for(NSString* title = otherButtonTitles; title != nil; title = va_arg(list, NSString*))
        {
            [self addButtonWithTitle:title];
        }

        va_end(list);

        _destructiveButtonTitle = destructiveButtonTitle;
    }

    return self;
}

- (void)setTintColor:(UIColor *)tintColor
{
    _customtintColor = tintColor;
}

-(void)tintColorDidChange
{
    [super tintColorDidChange];

    for(id subview in self.subviews)
    {
        if([subview isKindOfClass:[UIButton class]])
        {
            UIButton* button = subview;

            if(![button.titleLabel.text isEqualToString:_destructiveButtonTitle])
            {
                [button setTitleColor:_customtintColor forState:UIControlStateNormal];
            }
        }
    }
}

@end

Before showing, set the tint color of the action sheet to your liking.

In this implementation, I have elected to keep the destructive button title as red, but this can be changed.

Koal answered 24/10, 2013 at 20:56 Comment(0)
S
0

Please look at my child class UICustomActionSheet. I've just pushed the latest changes, which allow to display styling correctly for iOs6 and iOs7 design. https://github.com/gloomcore/UICustomActionSheet

You can set the colors, fonts, text colors and also images for each buttons. Works fine both for iPhone and iPad. Component is absolutely safety for Appstore so you can use it in your applications. Enjoy!

Sielen answered 12/2, 2014 at 22:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.