Describe the use and customization of UIAppearance for theming in UIKit.
Instruction: Explain how UIAppearance can be used to apply global themes to UI components in a UIKit-based app.
Updated
Official answer
UIAppearance records supported appearance customizations for instances of UIKit classes, optionally constrained by containment or traits. It is not a general object graph that allows every nested property to be edited. Only the documented appearance selectors participate; accessing UIButton's titleLabel through the appearance proxy is not a reliable way to set every button's font.
For navigation bars, use the dedicated appearance object. This complete UIKit helper configures ordinary and scroll-edge appearances; call it on the main actor during startup before the relevant bars enter a window:
import UIKit
@MainActor func configureNavigationTheme() {
let theme = UINavigationBarAppearance()
theme.configureWithOpaqueBackground()
theme.backgroundColor = .systemBackground
theme.titleTextAttributes = [.foregroundColor: UIColor.label]
let proxy = UINavigationBar.appearance()
proxy.standardAppearance = theme
proxy.scrollEdgeAppearance = theme
proxy.compactAppearance = theme
proxy.compactScrollEdgeAppearance = theme
}
The compact-scroll-edge property requires iOS 15 or later; omit or availability-guard it for older targets. The app delegate's launch setup or deliberate initialization before scene UI creation is an appropriate place. UISceneDelegate does not provide a willFinishLaunchingWithOptions callback.
Appearance changes are applied when a view enters a window, so changing the proxy is not a universal live-theme refresh for existing views. For runtime theme changes, update existing controls through a deliberate theme mechanism and use adaptive semantic colors. Configure button fonts through supported per-instance configuration or an owned component instead of chaining into a proxy's subview.
Test ordinary, compact and scroll-edge navigation states, per-instance overrides, multiple scenes, Dark Mode and accessibility settings. Appearance configuration does not replace layout, Dynamic Type or contrast verification, and DAU cannot establish that a theme was implemented correctly.
UIAppearance supported selectors and timing, Navigation bar appearance objects