【问题标题】:How do I change the button color in a UIAlertView如何更改 UIAlertView 中的按钮颜色
【发布时间】:2011-08-02 11:33:33
【问题描述】:

我有一个带有几个按钮的 UIAlertView。我想让某个按钮变成红色。我怎样才能做到这一点?

【问题讨论】:

    标签: iphone ios4 iphone-sdk-3.0


    【解决方案1】:

    虽然最初的问题是如何更改 UIAlertView 按钮颜色,但后来改为 UIActionSheet,以防万一有人要更改 UIActionSheet 按钮颜色,可以使用以下 UIActionSheet 委托方法:

    - (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
         for (UIView *aView in actionSheet.subviews) {
            UIButton *aButton = (UIButton *)aView;
            [aButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
    }
    

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 你当然可以,至少在 iOS7 和 iOS8 中是这样。在 Swift 中,这看起来像这样:class MyAlertView: UIAlertView { init(customParams: .....) { super.init(frame: CGRect.zeroRect) setup() } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) setup() } setup() { // Setup buttons, delegates...whatever you want } // More custom logic... }
      【解决方案3】:

      然后尝试 UIActionSheet 而不是 UIAlertView

      【讨论】:

      • 我切换到 UIActionSheet。我可以更改操作表按钮的颜色吗?
      • 参考说明: 破坏性按钮标题 破坏性按钮的标题。此按钮会自动添加到操作表并分配适当的索引,该索引可从 破坏性ButtonIndex 属性获得。此按钮显示为红色,表示它代表破坏性行为。如果您不想要破坏性按钮,请指定 nil。
      • 我已对其他按钮进行了编码。如何编码破坏按钮?
      • UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel Button" destructiveButtonTitle:@"Destructive Button" otherButtonTitles:@"Other Button 1", @"Other Button 2", nil];
      • 谢谢!我想做的是将破坏性ButtonTitle 命名为重新启动(没问题)。我的下一步是将 Destruct 按钮放在最后。我将如何对破坏按钮进行编码?我是否使用按钮索引 ==?
      【解决方案4】:

      不幸的是 UIAlerView 没有办法改变按钮的颜色。 如果您想要一个外观不同的 UIAlertView,您必须自己设计它并以模态视图的形式呈现。

      【讨论】:

        【解决方案5】:

        不幸的是,我认为您无法更改它。也许只能通过使用类别来覆盖,这绝对是苹果不鼓励的。但是,创建自定义 uialertview 非常容易。

        【讨论】:

          猜你喜欢
          • 2014-08-01
          • 1970-01-01
          • 2013-10-12
          • 1970-01-01
          • 2021-12-22
          相关资源
          最近更新 更多