简体   繁体   English

如何更改 UIAlertController 的色调颜色?

[英]How to change tint color of UIAlertController?

Can I change colors of the UIAlertController ?我可以更改UIAlertController颜色吗? A standard color is a blue color.标准颜色是蓝色。 And it's much close to the standard iOS apps.它非常接近标准的 iOS 应用程序。 If it's customizable?如果可以定制? How can I change colors of this?我怎样才能改变这个的颜色? For example a button color.例如按钮颜色。

Thanks!谢谢!

You could just change the tintColor of the underlying view, however, due to a known bug introduced in iOS 9 ( https://openradar.appspot.com/22209332 ), the tintColor is overridden by the application window's tintColor .您可以更改底层视图的tintColor ,但是,由于 iOS 9 中引入的已知错误 ( https://openradar.appspot.com/22209332),tintColor被应用程序窗口的tintColor覆盖。

You can either:您可以:

  1. Change the app tintColor in the AppDelegate.在 AppDelegate 中更改应用程序tintColor

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

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

In Swift, you could do something like this:在 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)

In Swift 4 and Xcode 9.2Swift 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)

只需更改基础视图的 tintColor 即可。

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

在 UIAllertController 中添加一行:

alert.view.tintColor = UIColor.black

To change the tint color for all Alerts in Swift:要在 Swift 中更改所有警报的色调颜色:

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

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

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