How to change tint color of UIAlertController?
Asked Answered
I

6

11

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 change colors of this? For example a button color.

Thanks!

Indigene answered 9/6, 2016 at 22:19 Comment(1)
Has a working method come up yet? All below answers are affected by the same bug.Washerman
S
29

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()
    })
    
Setsukosett answered 9/6, 2016 at 22:27 Comment(2)
Thanks! Works quite nice.Indigene
alertController.view.tintColor = [UIColor yellowColor]; Works just fine for meTrilley
A
13

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)
Anniceannie answered 9/6, 2016 at 23:7 Comment(4)
As @Setsukosett said, there's a bug that prevents alert controllers from honoring the tintColor that you set on them.Celisse
Looks like that bug has possibly been fixed. I just tested in Xcode 9.2 (9C40b) - iOS 11 and it seems to be working fine now.Latinity
@MarkMoeykens I see the bug in iOS 12.Washerman
@MarkMoeykens, setting the tint color works for me, but only changes the action text colors, not the title color. If you want to change the title color, you'll need to make a NSMutableAttributedString and then set the attributedTitle of the alert controller. And yeah, good to see you, Mark - even if I am five years late.Maun
L
5

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)
Lasonde answered 19/12, 2017 at 5:6 Comment(0)
B
4

Add one line in your UIAllertController :

alert.view.tintColor = UIColor.black
Billi answered 19/12, 2017 at 8:55 Comment(0)
M
3

Just change the tintColor of the underlying view.

[alertController.view setTintColor:[UIColor yellowColor]];
Malek answered 9/6, 2016 at 22:27 Comment(1)
This is actually doableTrilley
C
1

To change the tint color for all Alerts in Swift:

 extension UIAlertController{
    open override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
       self.view.tintColor = //color
    }
 }
Caracal answered 12/12, 2019 at 12:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.