How to correctly subclass UIControl?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Subclass UIControl when you want a reusable interactive component that should participate in target-action events like a built-in control. A correct subclass needs clear state management, predictable touch handling, and a clean contract for when it emits events such as .valueChanged.
Start With A Clear Control State Model
A custom control should centralize its state and update its appearance from that state:
That pattern keeps the control's visual output tied directly to its state instead of scattering rendering logic across several methods.
Handle Touch Tracking Deliberately
For UIControl, the tracking methods are usually the right place to interpret touches:
This gives behavior that feels much closer to system controls than bolting a tap gesture recognizer onto a plain UIView.
Expose Standard Control Events
The point of UIControl is not only touch handling. It is integration with the target-action system:
If your control represents a value change, emit .valueChanged consistently. If it behaves more like a button tap, .touchUpInside may be the relevant event instead.
That event contract is part of the control API and should be easy for other developers to understand.
Support Layout And Accessibility
A reusable control should participate cleanly in Auto Layout:
And it should expose accessibility metadata:
That keeps the control usable for VoiceOver and other assistive technologies.
Respect Built-In State Like isEnabled And isHighlighted
System controls change appearance when highlighted or disabled. Your custom control should do the same:
These inherited properties exist for a reason. Reusing them makes the control feel native.
Common Pitfalls
One common mistake is subclassing UIView and then manually re-creating control semantics that UIControl already provides.
Another issue is mutating internal state without emitting the correct control events, which makes the control hard to integrate with the rest of UIKit.
A third problem is combining layout logic, drawing logic, and touch logic in one large method instead of separating responsibilities.
Finally, custom controls often skip accessibility until late in the project, even though it should be designed in from the start.
Summary
- Subclass
UIControlwhen you want reusable target-action behavior, not just custom drawing. - Centralize state and update appearance from that state.
- Use the tracking methods for touch handling instead of ad hoc gesture logic.
- Emit the correct control events such as
.valueChanged. - Support layout, accessibility, and built-in states so the control behaves like a real UIKit component.
Related reading
- How to count occurrences of an element in a Swift array?
- How to create a circular ImageView in Android?
- How to Create a circular progressbar in Android which rotates on it?
- How to create a colored 1x1 UIImage on the iPhone dynamically?
- How to create a Custom Dialog box in android?
- How to create a delay in Swift?
- How to create a delay in Swift?
- How to create a DialogFragment without title?
.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.