How to change UIAlertController button text colour in iOS9?

后端 未结 14 1370
隐瞒了意图╮
隐瞒了意图╮ 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: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
    

提交回复
热议问题