TintColor UIActionSheet (or UIAlertView) (iOS 7+)

Is it possible to have buttons in a UIActionSheet in iOS 7 tintColor ? I mean, if my application is in the tintColor brand, for example, red, I do not want the blue buttons in the action sheet. Same thing with UIAlertView .

+6
source share
3 answers

I want to emphasize that this violates Apple's rules, but it 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]; } } }]; } 

(corresponds to UIActionSheetDelegate )

Not tried UIAlertView yet.

+5
source

It is possible. Here is a brief 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 displaying, set the hue color of the action sheet to your liking.

In this implementation, I decided to keep the name of the destructive button red, but this can be changed.

+1
source

Take a look at my child class UICustomActionSheet. I just clicked on the latest changes that allow the style to display correctly for iOs6 and iOs7 design. https://github.com/gloomcore/UICustomActionSheet

You can set colors, fonts, text colors, as well as images for each button. Works great for both iPhone and iPad. The component is absolutely safe for the Appstore, so you can use it in your applications. Enjoy it!

0
source

Source: https://habr.com/ru/post/956673/


All Articles