如何改变UIAlertController的色调?

Beg*_*ner 5 ios swift

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

谢谢!

gui*_*dev 17

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

你可以:

  1. tintColor在AppDelegate中更改应用程序.

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

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

  • alertController.view.tintColor = [UIColor yellowColor]; 对我来说工作得很好 (2认同)

小智 9

在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)
Run Code Online (Sandbox Code Playgroud)

  • 看起来这个bug可能已经修复了.我刚刚在Xcode 9.2(9C40b) - iOS 11中进行了测试,现在看起来工作正常. (3认同)
  • @MarkMoeykens 我看到了 iOS 12 中的错误。 (2认同)

Pra*_*wad 5

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)
Run Code Online (Sandbox Code Playgroud)