【问题标题】:tintColor of UIActionSheet (or UIAlertView) (iOS 7+)UIActionSheet(或 UIAlertView)的 tintColor (iOS 7+)
【发布时间】:2025-12-31 19:15:01
【问题描述】:

是否可以在 iOS 7 的 tintColor 颜色中使用 UIActionSheet 按钮?我的意思是,如果我的应用程序的品牌为tintColor,例如红色,我不想在操作表中出现蓝色按钮。与UIAlertView 相同。

【问题讨论】:

标签: ios cocoa-touch ios7 uiactionsheet


【解决方案1】:

我想强调一下,这违反了 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];
            }
        }
    }];
}

(符合UIActionSheetDelegate

尚未尝试 UIAlertView。

【讨论】:

  • 您应该使用以下方法来更新按钮标题颜色:[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
【解决方案2】:

这是可能的。这是 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

在显示之前,将操作表的色调设置为您喜欢的颜色。

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

【讨论】:

    【解决方案3】:

    请查看我的子类 UICustomActionSheet。 我刚刚推送了最新的更改,它允许为 iOs6 和 iOs7 设计正确显示样式。 https://github.com/gloomcore/UICustomActionSheet

    您可以为每个按钮设置颜色、字体、文本颜色以及图像。适用于 iPhone 和 iPad。组件对于 Appstore 来说是绝对安全的,因此您可以在您的应用程序中使用它。享受吧!

    【讨论】: