UIView
iOS development
resize event
Swift
event handling

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.

Browse interview questions

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:

swift
1final class CardView: UIView {
2    override func layoutSubviews() {
3        super.layoutSubviews()
4        print("New bounds:", bounds)
5    }
6}

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:

swift
1override func viewDidLayoutSubviews() {
2    super.viewDidLayoutSubviews()
3    print("Root view size:", view.bounds.size)
4}

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.

swift
1final class MeterView: UIView {
2    private var lastSize: CGSize = .zero
3
4    override func layoutSubviews() {
5        super.layoutSubviews()
6
7        guard bounds.size != lastSize else { return }
8        lastSize = bounds.size
9
10        print("Size really changed:", bounds.size)
11    }
12}

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

  • 'UIView has 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.