How to draw a custom UIView that is just a circle - iPhone app
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Drawing a circular view in iOS seems simple, but correct implementation depends on layout timing, constraint setup, and rendering strategy. Many bugs happen when radius is set before the final size is known, or when width and height are allowed to drift apart. A good solution starts with the simplest rendering technique that matches the actual design need.
Choose the Right Technique for the Job
There are three common ways to build a circle in UIKit:
- Layer corner radius for simple filled circles.
CAShapeLayerfor ring outlines and stroke animations.drawfor custom paint behavior.
Use the simplest technique that covers your requirements. Overengineering a static dot with custom drawing adds maintenance cost without adding any user-facing value.
Method 1: Corner Radius in layoutSubviews
For plain circles, set corner radius from current bounds inside layoutSubviews.
Why this works:
layoutSubviewsruns after Auto Layout calculates final size.- Radius updates correctly on rotation or constraint changes.
- GPU handles this path efficiently for most UI components.
Enforce Square Geometry with Constraints
A true circle requires equal width and height. Add explicit constraints to preserve shape.
Without the equal-size rule, the same view can become an ellipse on some layouts.
Method 2: CAShapeLayer for Ring and Border Effects
When you need a ring, progress arc, or animated stroke, CAShapeLayer gives better control.
This method avoids manual pixel math and supports smooth animation.
Method 3: Custom Paint in draw
Use draw when you need full visual control, such as gradients, multiple strokes, or dynamic paint logic.
Keep draw lightweight. Heavy computation there can hurt scrolling and animation performance.
Add Animation Safely
A simple rotation animation can make a circular indicator feel responsive.
If motion is decorative, respect reduced motion settings in accessibility preferences.
Reusability and Accessibility
For reusable components, expose simple configuration points such as fill color and border width. Also set accessibility behavior intentionally.
If the circle is purely decorative, set it as not accessible to avoid noise for assistive technologies.
Common Pitfalls
- Setting corner radius in
initbefore bounds are final. - Forgetting equal width and height constraints.
- Doing expensive operations repeatedly in
draw. - Animating many circle layers without checking performance impact.
- Ignoring accessibility roles for circles that convey important state.
Summary
- Use corner radius for the simplest and most performant circle view.
- Use
CAShapeLayerwhen ring styling or stroke animation is required. - Use custom
drawonly for visuals that cannot be achieved with layers. - Enforce square constraints to guarantee a true circle shape.
- Treat accessibility and motion behavior as part of component design.
Related reading
- How to draw a smooth circle with CAShapeLayer and UIBezierPath?
- How to draw a transparent UIToolbar or UINavigationBar in iOS7
- How to draw border around a UILabel?
- How to draw border on just one side of a linear layout?
- How to easily resize/optimize an image size with iOS?
- How to Edit Empty Spaces of Left, Right UIBarButtonItem in UINavigationBar iOS 7
- How to embed small icon in UILabel
- How to emulate GPS location in the Android Emulator?
.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.