UIActionSheet(或UIAlertView)的tintColor(iOS 7+)

use*_*500 6 cocoa-touch uiactionsheet ios ios7

是否可以使用UIActionSheetiOS 7的tintColor颜色按钮?我的意思是如果我的应用程序是品牌tintColor,例如红色,我不想在行动表中使用蓝色按钮.同样的UIAlertView.

Ada*_*ite 5

我想强调这违反了Apple的规则,但这有效:

- (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];
            }
        }
    }];
}
Run Code Online (Sandbox Code Playgroud)

(符合UIActionSheetDelegate)

尚未尝试过UIAlertView.

  • 您应该使用以下方法更新按钮标题颜色:[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; (4认同)

Leo*_*ica 1

有可能的。这是 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
Run Code Online (Sandbox Code Playgroud)

在显示之前,根据您的喜好设置操作表的色调颜色。

在此实现中,我选择将破坏性按钮标题保留为红色,但这可以更改。