简体   繁体   中英

iOS UIAlertController Dark Theme

I'm working on an app for iOS with a dark theme and I want to use a UIAlertController . However, the white alert looks out of place. Would it be against the Human Interface Guidelines even on a dark themed app to use a dark themed UIAlertController (when I say dark themed alert all I mean is switching the white background to a dark grey and the text to white this also includes the buttons).

Assuming it's not against the guidelines how would I go about achieving this? I can't seem to find a theme for the UIAlertController that would allow this.

With iOS 13, Apple provides Dark Mode. But if your App theme doesn't match with Device Theme, you can set your view controller's theme.

For eg, My Device Appearance is Dark Mode, and my App is always in light mode. But the popups using UIAlertController are following Device Theme, Dark theme, and a black popup over my white screen doesn't match.

Solution #1: Inline Solution

While creating the UIAlertController, I can set its theme

alert.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;

Solution #2: Support for Whole App

In you AppDelegate, set the theme for window

self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;

This will cause all the popups to use light theme.

Note: UIAlertView won't use the dark/light theme if you set its overrideUserInterfaceStyle property. Also, don't forget to add if #available(iOS 13.0, *) check before setting the property, as it is available only in iOS 13+

Well, customizing UIAlertView / UIAlertController is not possible, but there's a plenty of custom Alert implementations on CocoaControls, just take a look there. It's more a philosophical question; I guess it doesn't really violate anything.

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