Contents

UIAppearance

A collection of methods that gives you access to the appearance proxy for a class.

Declaration

@MainActor protocol UIAppearance : NSObjectProtocol

Overview

You can customize the appearance of instances of a class by sending appearance-modification messages to the class’s appearance proxy.

There are two ways to customize appearance for objects: for all instances, and for instances contained within an instance of a container class.

To customize the appearance of all instances of a class, use appearance() to get the appearance proxy for the class. For example, to modify the bar background tint color for all instances of UINavigationBar:

UINavigationBar.appearance().barTintColor = navBarTintColor

To customize the appearances for instances of a class when contained within an instance of a container class, or instances in a hierarchy, use appearanceWhenContainedIn: to get the appearance proxy for the class. For example, to modify the appearance of bar buttons, based on the object that contains the navigation bar:

let navigationBarAppearance =
UINavigationBar.appearance(whenContainedInInstancesOf: [UINavigationController.self])
navigationBarAppearance.setBackgroundImage(navBarBackgroundImage, for: .any, barMetrics: .default)

let barButtonNavigationBarAppearance =
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self])
barButtonNavigationBarAppearance.setBackgroundImage(barButtonNavBarImage, for: .normal, barMetrics: .default)

let barButtonToolbarAppearance =
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UIToolbar.self])
barButtonToolbarAppearance.setBackgroundImage(barButtonToolbarImage, for: .normal, barMetrics: .default)

In any given view hierarchy, the outermost appearance proxy wins. Specificity (depth of the chain) is the tie-breaker. In other words, the containment statement in appearanceWhenContainedIn: is treated as a partial ordering. Given a concrete ordering (actual subview hierarchy), UIKit selects the partial ordering that’s the first unique match when reading the actual hierarchy from the window down.

You can further refine which instances of a class will have their appearance customized by specifying a trait collection. Use the appearance(for:) and appearanceForTraitCollection:whenContainedIn: methods to retrieve the proxy for a class with the specified trait collection.

To support appearance customization, a class must conform to the UIAppearanceContainer protocol and relevant accessor methods must be marked with UI_APPEARANCE_SELECTOR.

Topics

Working with the appearance proxy

See Also

Appearance proxies