Is there a UIView resize event?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UIView does not have a dedicated "resize event" in the same sense that some UI frameworks expose one. In UIKit, size changes are normally observed through layout callbacks, constraint updates, or controller lifecycle methods that run when bounds and layout change.
The Closest Thing: layoutSubviews
For a custom UIView, the most common hook is layoutSubviews(). UIKit calls it whenever the view needs to lay out its subviews, which includes many situations where size has changed.
Swift example:
This is the usual answer when you want your view to react to its own size changes.
Why There Is No Single Resize Event
UIKit is layout-driven. A view can change size because of:
- Auto Layout constraint updates
- rotation
- split view resizing
- safe area changes
- parent view layout passes
- animations changing frames or transforms
Because layout can change for many reasons, UIKit exposes layout callbacks rather than one dedicated resize notification.
View Controller Hooks
Sometimes the view itself is not the right place to react. If the size change matters at the controller level, use methods such as:
- '
viewDidLayoutSubviews()' - '
viewWillTransition(to:with:)' - '
traitCollectionDidChange(_:)'
Example:
This is especially useful when a controller manages several subviews and the layout reaction belongs to the whole screen, not one specific view.
Auto Layout Changes the Style of the Solution
With Auto Layout, the best answer is often not "listen for resize and manually reposition everything." It is usually better to express the layout in constraints and let UIKit recalculate sizes automatically.
Then use layout callbacks only for the parts that truly depend on final measured dimensions, such as:
- recalculating a gradient layer frame
- adjusting a corner radius tied to height
- updating a custom drawing path
That keeps your code aligned with UIKit’s layout system instead of fighting it.
Detecting Actual Size Changes
layoutSubviews() can be called multiple times even when the size is the same. If you only want to run code when the bounds actually changed, cache the old size.
This avoids redundant work during repeated layout passes.
Other Less Common Options
You can observe frame or bounds indirectly in some designs, but this is usually less clean than using layout callbacks. KVO-style observation is rarely the best answer for ordinary view resizing in UIKit.
Likewise, frame-based manual layout is still possible, but if the app already uses Auto Layout, staying inside the layout lifecycle is much simpler.
Common Pitfalls
The biggest mistake is expecting a dedicated didResize callback on UIView. UIKit does not expose that exact abstraction.
Another issue is doing heavy work in every layoutSubviews() call without checking whether size actually changed. Layout can happen often.
Developers also sometimes mix manual frame changes with Auto Layout constraints and then wonder why the view seems to resize unpredictably.
Finally, choose the correct scope. Use layoutSubviews() for view-specific reactions and controller lifecycle methods when the whole screen layout is what matters.
Summary
- '
UIViewhas no dedicated resize event.' - '
layoutSubviews()is the usual place to react to size changes inside a custom view.' - '
viewDidLayoutSubviews()and related controller methods are useful at the screen level.' - With Auto Layout, prefer constraints first and use callbacks only for size-dependent logic.
- Cache the previous bounds if you need to react only to real size changes.
Related reading
- Is there a unique Android device ID?
- Is there a way to automate the Android SDK installation?
- Is there a way to create xxhdpi, xhdpi, hdpi, mdpi and ldpi drawables from a large scale image?
- Is there a way to get all values in NSUserDefaults?
- Is there a way to get the source code from an APK file?
- Is there a way to get the source code from an APK file?
- Is there a way to hide the scroll indicators in a UIScrollView?
- Is there a way to pretty print Swift dictionaries to the console?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.