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.

Brian Sachetta
  • 3,319
  • 2
  • 34
  • 45
user500
  • 4,519
  • 6
  • 43
  • 56

3 Answers3

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.

Adam Waite
  • 19,175
  • 22
  • 126
  • 148
  • 4
    You should be using the following method to update the button title color: [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; – Brody Robertson Mar 12 '14 at 19:42
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.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
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!

Gloomcore
  • 940
  • 7
  • 11