Swift subclass UIView
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Subclassing UIView is the standard UIKit way to build a reusable custom visual component. It is the right tool when you need custom drawing, a self-contained layout, or a view with behavior that would be awkward to assemble repeatedly from stock controls.
Start With the Right Initializers
A custom view should initialize correctly whether it is created in code or loaded from a storyboard or XIB. That usually means implementing both init(frame:) and init(coder:), then calling a shared setup method.
This pattern prevents duplicated setup logic and makes the class reliable in multiple creation paths.
Know Which Method to Override
A lot of confusion around UIView subclassing comes from overriding the wrong method for the job.
Use layoutSubviews() when the subview frames or constraints need adjustment after the view’s bounds change.
Use draw(_:) when you need custom Core Graphics drawing.
Use intrinsicContentSize when the view knows its natural size and should participate in Auto Layout without fixed external constraints.
These methods solve different problems. For example, drawing a border in draw(_:) and repositioning labels in layoutSubviews() is normal. Rebuilding the whole subview hierarchy in draw(_:) is usually a mistake.
Custom Drawing Example
If the view’s appearance is mostly graphical rather than compositional, draw(_:) may be the cleanest approach.
Custom drawing is powerful, but it should be used deliberately. If a view can be composed from standard subviews and layers, that is often easier to maintain.
Auto Layout and Sizing
Modern UIKit code should usually be constraint-driven. Avoid hard-coding geometry too early in initialization because the view’s size is not final at that stage.
If your custom view has a natural size, provide it explicitly.
If the layout depends on dynamic content, call invalidateIntrinsicContentSize() after updating the relevant data.
This is often a better design than exposing frame math to every caller of the view.
When a UIView Subclass Is the Right Choice
A dedicated subclass is a good fit when:
- the component is reused in many screens
- the view manages its own state and visual updates
- drawing or layout is complex enough to deserve isolation
- the public API can stay small and predictable
A subclass is usually the wrong choice when you only need a one-off screen layout or when a standard UIStackView, UILabel, or UIButton composition already solves the problem cleanly.
Common Pitfalls
The most common mistake is doing layout work in init that depends on the final size of the view. Bounds are often not correct there yet.
Another common issue is overriding draw(_:) for work that is not actually drawing. Subview creation, Auto Layout setup, and data binding belong elsewhere.
Developers also sometimes forget the init(coder:) path, which causes storyboard-loaded views to crash or skip initialization.
Finally, avoid turning a custom view into a miniature view controller. Keep its API focused on presentation and user interaction, and let higher layers own navigation and business logic.
Summary
- Subclass
UIViewwhen you need a reusable component with custom layout, drawing, or behavior. - Implement both initializer paths and funnel setup through a shared method.
- Override
layoutSubviews(),draw(_:), andintrinsicContentSizeonly for their intended responsibilities. - Prefer Auto Layout-friendly design over premature frame math.
- Keep the custom view focused so it remains reusable and easy to test.
Related reading
.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.