简体   繁体   English

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

[英]In iOS 7, how do I change the color of the options in my UIActionSheet?

I use green as the "action color" throughout my app, and I want the options in my UIActionSheets to be green as well, for consistency. 我在整个应用程序中使用绿色作为“动作颜色”,我希望我的UIActionSheets中的选项也是绿色的,以保持一致性。 How can I change the colour of the UIActionSheet options to green from blue? 如何将UIActionSheet选项的颜色从蓝色更改为绿色?

Utilize the willPresentActionSheet delegate method of UIActionSheet to change the action sheet button color. 利用UIActionSheetwillPresentActionSheet委托方法更改操作表按钮颜色。

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

You could do something like this: 你可以这样做:

// 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];

    }
}

If you're going to reuse this a lot, I'd subclass UIActionSheet and use this code. 如果您要重复使用它,我会将UIActionSheet子类化并使用此代码。

You can change easily the application's tint color using this short function on your AppDelegate didFinishLaunchingWithOptions method. 您可以使用AppDelegate didFinishLaunchingWithOptions方法上的这个简短函数轻松更改应用程序的色调颜色。

[[UIView appearance] setTintColor:[UIColor redColor]];

Hope it will help you :) 希望它能帮到你:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM