【问题标题】:In iOS 7, how do I change the color of the options in my UIActionSheet?在 iOS 7 中,如何更改 UIActionSheet 中选项的颜色?
【发布时间】:2013-10-06 18:27:19
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c ios7 uiactionsheet


    【解决方案1】:

    利用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];
            }
        }
    }
    

    【讨论】:

    • 这个答案更好。使用委托方法更简洁。上面的答案也不会改变取消按钮的颜色。
    • 由于某种原因,如果你也想改变字体,你必须在改变颜色之前这样做,否则它不会起作用
    • @BrenoGazzola:会发生什么?
    • 在执行button.titleLabel.textColor = MY_COLOR;button.titleLabel.font = MY_FONT; 时,选项已更改为新字体,但仍保持蓝色和红色。只需将顺序更改为 button.titleLabel.font = MY_FONT;button.titleLabel.textColor = MY_COLOR; 即可解决问题,我得到了新字体和新颜色。
    • 这很奇怪。您正在运行哪个版本的 iOS 以及您使用什么设备/模拟器来运行代码?如果今晚我能复制这个,我会向苹果提交错误票。
    【解决方案2】:

    你可以这样做:

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

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

    【讨论】:

    • 那是使用私有 API 吗?
    • 不,它只是访问 UIActionSheet 的所有子视图并找到按钮
    • 当你点击它时它会变回蓝色。
    • 您是否设法使其适用于取消按钮?我的一直是以前的颜色
    【解决方案3】:

    您可以使用 AppDelegate didFinishLaunchingWithOptions 方法上的这个简短函数轻松更改应用程序的色调。

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

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 2013-09-26
      • 2014-03-03
      • 1970-01-01
      • 2013-10-14
      相关资源
      最近更新 更多