UIActionSheet(或UIAlertView)的颜色(iOS 7+;)

UIActionSheet(或UIAlertView)的颜色(iOS 7+;),ios,cocoa-touch,ios7,uiactionsheet,Ios,Cocoa Touch,Ios7,Uiactionsheet,在iOS 7的tintColorcolor中是否可以在UIActionSheet中设置按钮?我的意思是,如果我的应用程序使用的是品牌tintColor,例如红色,我不希望在操作表中使用蓝色按钮。与UIAlertView相同,这是可能的。下面是iOS7的快速实现: @interface LNActionSheet : UIActionSheet { NSString* _destructiveButtonTitle; UIColor* _customtintColor; } @e

在iOS 7的
tintColor
color中是否可以在
UIActionSheet
中设置按钮?我的意思是,如果我的应用程序使用的是品牌
tintColor
,例如红色,我不希望在操作表中使用蓝色按钮。与
UIAlertView

相同,这是可能的。下面是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
@接口LActionSheet:UIActionSheet
{
NSString*_破坏性按钮;
UIColor*\u定制色彩;
}
@结束
@实施行动表
-(id)initWithTitle:(NSString*)title委托:(id)委托CancelButtonTile:(NSString*)CancelButtonTile Destructive ButtonTile:(NSString*)Destructive ButtonTile其他ButtonTiles:(NSString*)其他ButtonTiles。。。
{
self=[super initWithTitle:title委托:委托CancelButtonTile:CancelButtonTile Destructive ButtonTile:DestructiveButtonTile其他ButtonTiles:nil];
如果(自我)
{
va_列表;
va_开始(列表、其他按钮);
对于(NSString*title=otherButtonTitles;title!=nil;title=va_arg(列表,NSString*))
{
[self addButton with title:title];
}
va_end(列表);
_DestructiveButtontile=DestructiveButtontile;
}
回归自我;
}
-(void)setTintColor:(UIColor*)tintColor
{
_customtintColor=tintColor;
}
-(无效)更改
{
[超级改变];
for(self.subview中的id子视图)
{
if([subview iskindof类:[UIButton类]])
{
UIButton*按钮=子视图;
如果(![button.titleLabel.text IseQualtString:\u DestructiveButtonTile])
{
[按钮设置标题颜色:_CustomTintColorfor状态:uicontrol状态正常];
}
}
}
}
@结束
在显示之前,根据您的喜好设置动作表的色调


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

我想强调的是,这违反了苹果的规则,但这是可行的:

- (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。

请查看我的子类UICustomActionSheet。 我刚刚推出了最新的更改,它允许在iOs6和iOs7设计中正确显示样式。


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

John Riselvato正确地链接到答案:“不,UIAlertView或UIActionSheet都不是由Apple设计为子类的。”对于自定义外观,您需要自己滚动。您应该使用以下方法更新按钮标题颜色:[按钮设置标题颜色:[UIColor greenColor]For状态:UIControlStateNormal];