How to change tint color of UIAlertController?

后端 未结 6 995
暗喜
暗喜 2020-12-06 03:25

Can I change colors of the UIAlertController ? A standard color is a blue color. And it\'s much close to the standard iOS apps. If it\'s customizable? How can I

相关标签:
6条回答
  • 2020-12-06 03:32

    Add one line in your UIAllertController :

    alert.view.tintColor = UIColor.black
    
    0 讨论(0)
  • 2020-12-06 03:38

    To change the tint color for all Alerts in Swift:

     extension UIAlertController{
        open override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
           self.view.tintColor = //color
        }
     }
    
    0 讨论(0)
  • 2020-12-06 03:39

    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.

    You can either:

    1. Change the app tintColor in the AppDelegate.

      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()
      })
      
    0 讨论(0)
  • 2020-12-06 03:40

    In Swift, you could do something like this:

    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)
    
    0 讨论(0)
  • 2020-12-06 03:44

    Just change the tintColor of the underlying view.

    [alertController.view setTintColor:[UIColor yellowColor]];
    
    0 讨论(0)
  • 2020-12-06 03:58

    In Swift 4 and 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)
    
    0 讨论(0)
提交回复
热议问题