如何改变UIAlertController的色调?

时间:2016-06-09 22:19:43

标签: ios swift

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

谢谢!

6 个答案:

答案 0 :(得分:14)

但是,您可以更改基础视图的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()
    })
    

答案 1 :(得分:6)

在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)

答案 2 :(得分:2)

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

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

答案 3 :(得分:2)

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)

答案 4 :(得分:1)

在UIAllertController中添加一行:

alert.view.tintColor = UIColor.black

答案 5 :(得分:0)

要更改Swift中所有警报的颜色,请按以下步骤操作:

v1.x.x