在iOS 7中,如何更改UIActionSheet中选项的颜色?

Dou*_*ith 10 objective-c uiactionsheet ios ios7

我在整个应用程序中使用绿色作为"动作颜色",我希望我的UIActionSheets中的选项也是绿色的,以保持一致性.如何将UIActionSheet选项的颜色从蓝色更改为绿色?

Ste*_*vin 28

利用willPresentActionSheet委托方法UIActionSheet更改操作表按钮颜色.

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    for (UIView *subview in actionSheet.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            button.titleLabel.textColor = [UIColor greenColor];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这个答案更好.使用委托方法更不简洁.此外,上述答案不会更改取消按钮的颜色. (3认同)

Mat*_*ang 18

你可以这样做:

// Your code to instantiate the UIActionSheet
UIActionSheet *actionSheet = [[UIActionSheet alloc] init];
// Configure actionSheet

// Iterate through the sub views of the action sheet
for (id actionSheetSubview in actionSheet.subviews) {
    // Change the font color if the sub view is a UIButton
    if ([actionSheetSubview isKindOfClass:[UIButton class]]) {
        UIButton *button = (UIButton *)actionSheetSubview;
        [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];

    }
}
Run Code Online (Sandbox Code Playgroud)

如果您要重复使用它,我会将UIActionSheet子类化并使用此代码.