简体   繁体   中英

Change UIAlertController tintColor globally Xcode 11/iOS13

Changing the tintColor of the UIAlertController s used to be possible to change via the window 's tintColor . This was usually done in the AppDelegate and it would affect UIAlertController s tintColor as well. Unfortunately in projects started with Xcode 11 for iOS SDK 13 there is no window in the AppDelegate anymore.

Where do I need to set the tintColor , so that all UIAlertController s use it?

Things I tried:

  • setting the tintColor of the first UIViewController 's .view.window . Unfortunately the window is nil .
  • setting the main Storyboard's globalTint color. This affects the navigation bars and is also inherited by other UI elements, but not by the UIAlertController.
  • It would be an option to write an extension to the UIAlertController. But this not the clean way. There must be a better way.
  • I checked several answers to similar questions but for older Xcode versions. The answers do not apply.

Thank you for your help!

Try this to get access to the window using windowScene for iOS 13+:

let windowScene = UIApplication.shared
                .connectedScenes
                .filter { $0.activationState == .foregroundActive }
                .first
if let windowScene = windowScene as? UIWindowScene {
    window = UIWindow(windowScene: windowScene) // now you've access to the window
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM