UIViewController: traitchange event
The traitchange event fires when the view controller's appearance traits change, such as switching to dark mode or changing the text size.
Syntax
page.addEventListener('traitchange', (event) => { })Event object
UITraitChangeEvent. Besides type and target (this view controller), it has one more read-only field:
| Field | Type | Meaning |
|---|---|---|
previousTraitCollection | UITraitCollection | The set of values before the change. Read the current values from event.target.traitCollection |
Not cancelable.
When it fires
When any value in the view controller's traitCollection changes, including when overrideUserInterfaceStyle is changed. For which changes fire it and what to watch for, see Appearance traits.
Examples
js
const page = new UIViewController();
page.addEventListener('traitchange', (event) => {
const { previousTraitCollection } = /** @type {UITraitChangeEvent} */ (event);
console.log(previousTraitCollection.userInterfaceStyle, '→',
page.traitCollection.userInterfaceStyle);
});
page.overrideUserInterfaceStyle = 'dark';Effect: When the view controller is on screen and was light, prints light → dark. Not verified in the simulator.
Corresponding iOS API
-[UIViewController traitCollectionDidChange:], and from iOS 17, registerForTraitChanges:.