viewIsAppearing(_:) — A New Addition to “UIViewController” class in iOS
In Swift, view controllers are responsible for managing views and how they appear on the screen. We’ve been using several view lifecycle methods like:
viewWillAppear(_), viewDidAppear(_), viewWillDisappear(_), and viewDidDisappear(_)
iOS calls these methods at different points in the view controller’s lifecycle.
This quick post explores a new addition to the view lifecycle methods: viewIsAppearing(_)
Join the Swiftable Community to connect with iOS Engineers.
`viewIsAppearing(_)` in iOS 17
Introduced in iOS 17 and Xcode 15, `viewIsAppearing(_)` is a new method added to the `UIViewController` class. It’s designed to help us fine-tune how our views look when they become visible.
The Sequence in the View Lifecycle
- viewWillAppear(_:): This method is called first, indicating that the view will appear soon, but its size and position might not be final.
- View Updates: The view gets its final layout and position.
- viewIsAppearing(_): This method is called after the view has its final dimensions.
Use case
The ideal use cases for `viewIsAppearing(_)` are when you need to make UI adjustments that depend on the most up-to-date information about the view’s size and location on the screen.
For example, if you have an image that needs to resize itself perfectly to fit within the view’s bounds, `viewIsAppearing(_)` is the ideal place to do that because you’re guaranteed to have the most accurate size information at that point.
Here’s a Quick Comparison
| Method | When It's Called | Use For |
|------------------|-----------------------------|---------------------------|
| `viewWillAppear` | Before the view appears, | Actions before animations |
| | layout might be preliminary |eg: setting up transitions |
| | | |
| `viewIsAppearing`| After the view has its final| UI adjustments based on |
| | layout and position | final view size & position|
Sample Code
class MyViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("View will appear (layout might be preliminary)")
}
override func viewIsAppearing(_ animated: Bool) {
super.viewIsAppearing(animated)
print("View is appearing (layout is finalized)")
}
}
Conclusion
The viewIsAppearing(_:) function is a valuable addition for making UI adjustments based on the view’s final state. You can use it alongside viewWillAppear(_:) to manage different aspects of the view’s appearance. Give it a try in your project and see how it enhances your UI management.
Happy coding!