How to change UIAlertController button text colour in iOS9?

后端 未结 14 1265
隐瞒了意图╮
隐瞒了意图╮ 2020-12-15 16:16

The question is similar to iOS 8 UIActivityViewController and UIAlertController button text color uses window's tintColor but in iOS 9.

I have a UIAlertControlle

相关标签:
14条回答
  • 2020-12-15 16:40

    You can change it using: Swift 3.x

        strongController.view.tintColor = UIColor.green
    
    0 讨论(0)
  • 2020-12-15 16:41

    The most reasonable way is set the main window's tintColor. As a uniform appearance is what we usually need.

    // in app delegate
    window.tintColor = ...
    

    Other solutions have defects

    • Use apperance

      UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = ...
      

      Not works on iOS 9, tests with iOS 11 SDK.

      [[UIView appearance] setTintColor:[UIColor black]];  
      

      Are you serious?

    • Set UIAlertController view's tintColor is unstable. The color may change when user press the button or after view layout.

    • Subclass UIAlertController and overwrite layout method is hack way which is unacceptable.

    0 讨论(0)
  • 2020-12-15 16:42

    I was able to solve this by subclassing UIAlertController:

    class MyUIAlertController: UIAlertController {
    
        override func viewWillLayoutSubviews() {
            super.viewWillLayoutSubviews()
    
            //set this to whatever color you like...
            self.view.tintColor = UIColor.blackColor()
        }
    }
    

    This survives a device rotation while the alert is showing.

    You also don't need to set the tintColor after presenting the alert when using this subclass.

    Though it isn't necessary on iOS 8.4, this code does work on iOS 8.4 as well.

    Objective-C implementation should be something like this:

    @interface MyUIAlertController : UIAlertController
    @end
    
    @implementation MyUIAlertController
    -(void)viewWillLayoutSubviews {
        [super viewWillLayoutSubviews];
    
        //set this to whatever color you like...
        self.view.tintColor = [UIColor blackColor];
    }
    @end
    
    0 讨论(0)
  • 2020-12-15 16:43

    swift3

    Tried to use UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = MyColor but this prevents other items unrelated to the UIAlertController from tintColor configuration. I saw it while trying to change the color of navigation bar button items.

    I switched to an extension (based on Mike Taverne's response above) and it works great.

    extension UIAlertController {
    
    override open func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
    
        //set this to whatever color you like...
        self.view.tintColor = MyColor
    }
    }
    
    0 讨论(0)
  • 2020-12-15 16:43

    After alot of research, I found out how to make this work:

    let cancelButton = UIAlertAction(title: button, style: UIAlertAction.Style.cancel, handler: { (action) in alert.dismiss(animated: true, completion: nil)
        })
    
        cancelButton.setValue(UIColor.systemBlue, forKey: "titleTextColor")
        alert.addAction(cancelButton)
    

    Just change the UIColor.systemBlue to what ever color you want, and it will make just that button a special color. I made this example (I created 3 UIAlertActions to make it.):

    With just UIAlertAction.Style.whatever, it can only make it blue or red. If you change the UIColor, it will make it any color you want!

    0 讨论(0)
  • 2020-12-15 16:43
    [[UIView appearance] setTintColor:[UIColor black]];  
    

    this will change all the UIView tintColor as well as UIAlertController's view

    0 讨论(0)
提交回复
热议问题