In WWDC23, Apple introduced a nuanced addition to the UIViewController lifecycle:
viewIsAppearing. This instance method is a game-changer for developers looking to fine-tune the presentation and layout of their views. Let’s explore how this method enhances the way we build responsive and dynamic interfaces.
The Role of viewIsAppearing
viewIsAppearing is invoked after the view has been inserted into the view hierarchy but before it’s laid out, making it an ideal place for last-minute UI updates. Its importance is most evident when dealing with dynamic content that depends on accurate view geometry. Not to forget,
viewIsAppearing is called right after
Why viewIsAppearing Matters?
Accurate Geometry: By the time
viewIsAppearing is called, the view has accurate geometry, including size and safe area insets.
Updated Traits: The trait collections of the view and view controller are updated, which is crucial for adapting UI based on environmental changes, like Dark Mode or accessibility settings.
Layout Established: The superview has laid out the view, making it the right place to make final adjustments or scroll a table view to a specific position.
When to Use viewIsAppearing?
Here’s a practical guide to choosing between
Use viewWillAppear(_:) for:
Pre-Transition Setup: If you need to prepare something before the view transition begins, like adding alongside animations via the transitionCoordinator. Balanced Callbacks: For actions not dependent on the traits or geometry of the view, like setting up or tearing down notifications, observers, or other non-UI related tasks.
Use viewIsAppearing(_:) for:
Trait and Geometry-Dependent Updates: When you need the updated traits and geometry, for example, adjusting UI elements that depend on the size or layout of the view. Post-Layout Adjustments: If you need to make changes after the view has been laid out by its superview, such as scrolling to a specific position in a UIScrollView.
When overriding this method, it’s crucial to call
super.viewIsAppearing(_:) to maintain the integrity of the lifecycle events. Below is an example illustrating the usage of viewIsAppearing:
In this scenario, the viewIsAppearing method ensures that the collection view scrolls to the new item only after all layout information is accurately determined.
viewIsAppearing offers a strategic point in the view lifecycle for making UI adjustments. By understanding the precise timing of its call, developers can ensure that their UI changes are made with the most up-to-date information about the view’s traits and geometry. Use this method to polish the details of your user interface, making your app feel more responsive and intuitive.
About the author
- Rizwan Ahmed - Senior iOS Engineer.
Twitter - https://twitter.com/rizwanasifahmed
- Exploring the New Push Notifications Console from Apple
- How to send Remote Push Notifications to an iOS Simulator with Xcode 14
- How to Instantly Track a Variable’s Value Changes with Xcode Watchpoints
- How to add a loader to an UIButton
- How to filter screenshots, cinematic videos, and depth-effect photos in PHPickerViewController
- Implementing a custom native calendar using UICalendarView in iOS16 and Swift
- Exploring Deque in Swift Collections
- Replacing UIImagePickerController with PHPickerViewController
Zoho ToDo: Get work organised
Zoho ToDo is the ultimate task management application for all your personal and work tasks.
With clean views, personal and group tasks, categories, Kanban boards, social-media style collaboration, and mobile-exclusive features, you'll start enjoying every task you do!
Like our articles? Support us!