【发布时间】:2019-12-12 20:41:38
【问题描述】:
我可以更改 UIAlertController 的颜色吗?标准颜色是蓝色。它非常接近标准的 iOS 应用程序。如果可以定制?我怎样才能改变这个颜色?例如按钮颜色。
谢谢!
【问题讨论】:
-
工作方法出来了吗?以下所有答案都受到同一个错误的影响。
我可以更改 UIAlertController 的颜色吗?标准颜色是蓝色。它非常接近标准的 iOS 应用程序。如果可以定制?我怎样才能改变这个颜色?例如按钮颜色。
谢谢!
【问题讨论】:
您可以只更改底层视图的 tintColor,但是,由于 iOS 9 中引入的一个已知错误 (https://openradar.appspot.com/22209332),tintColor 会被应用程序窗口的 tintColor 覆盖。
您可以:
在 AppDelegate 中更改应用 tintColor。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
self.window.tintColor = UIColor.redColor()
return true
}
在完成块中重新应用颜色。
self.presentViewController(alert, animated: true, completion: {() -> Void in
alert.view.tintColor = UIColor.redColor()
})
【讨论】:
在 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)
【讨论】:
tintColor。
在 Swift 4 和 Xcode 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
【讨论】:
要更改 Swift 中所有警报的色调颜色:
extension UIAlertController{
open override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.view.tintColor = //color
}
}
【讨论】: