【问题标题】:How to change tint color of UIAlertController?如何更改 UIAlertController 的色调颜色?
【发布时间】:2019-12-12 20:41:38
【问题描述】:

我可以更改 UIAlertController 的颜色吗?标准颜色是蓝色。它非常接近标准的 iOS 应用程序。如果可以定制?我怎样才能改变这个颜色?例如按钮颜色。

谢谢!

【问题讨论】:

  • 工作方法出来了吗?以下所有答案都受到同一个错误的影响。

标签: ios swift


【解决方案1】:

您可以只更改底层视图的 tintColor,但是,由于 iOS 9 中引入的一个已知错误 (https://openradar.appspot.com/22209332),tintColor 会被应用程序窗口的 tintColor 覆盖。

您可以:

  1. 在 AppDelegate 中更改应用 tintColor

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
        self.window.tintColor = UIColor.redColor()
        return true
    }
    
  2. 在完成块中重新应用颜色。

    self.presentViewController(alert, animated: true, completion: {() -> Void in
        alert.view.tintColor = UIColor.redColor()
    })
    

【讨论】:

  • 谢谢!效果很好。
  • alertController.view.tintColor = [UIColor yellowColor];对我来说很好用
【解决方案2】:

在 Swift 中,你可以这样做:

let alert = UIAlertController(title: "Alert", message: "This is an alert.", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
alert.view.tintColor = UIColor.redColor()
self.presentViewController(alert, animated: true, completion: nil)

【讨论】:

  • 正如@guidev 所说,有一个错误会阻止警报控制器尊重您设置的tintColor
  • 看起来该错误可能已修复。我刚刚在 Xcode 9.2 (9C40b) - iOS 11 中进行了测试,现在它似乎工作正常。
  • @MarkMoeykens 我在 iOS 12 中看到了这个错误。
【解决方案3】:

Swift 4Xcode 9.2

let alertView = UIAlertController(title: "", message: "", preferredStyle: .alert)

alertView.addAction(UIAlertAction(title: "CONFIRM", style: .default, handler: { (alertAction) -> Void in
                //my logic
            }))

alertView.addAction(UIAlertAction(title: "CANCEL", style: .default, handler: nil))


alertView.view.tintColor = UIColor.init(red: 45.0/255.0, green: 187.0/255.0, blue: 135.0/255.0, alpha: 1.0)

present(alertView, animated: true, completion: nil)

【讨论】:

    【解决方案4】:

    只需更改底层视图的 tintColor。

    [alertController.view setTintColor:[UIColor yellowColor]];
    

    【讨论】:

    • 这其实是可行的
    【解决方案5】:

    在你的 UIAllertController 中添加一行:

    alert.view.tintColor = UIColor.black
    

    【讨论】:

      【解决方案6】:

      要更改 Swift 中所有警报的色调颜色:

       extension UIAlertController{
          open override func viewDidLayoutSubviews() {
              super.viewDidLayoutSubviews()
             self.view.tintColor = //color
          }
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多