How do I draw a circle in iOS Swift?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In UIKit, drawing a circle is not about a special circle API. It is about choosing the right drawing surface. Most of the time, CAShapeLayer is the cleanest solution because it is easy to style, resize, and animate, while custom drawing in draw(_:) is better when the circle is just one element in a larger rendered view.
Use CAShapeLayer for Most UI Circles
A circle is simply an oval inside a square rectangle. With CAShapeLayer, you describe that path once and let Core Animation handle the rendering:
This approach is flexible. You can change stroke color, fill color, dash style, and animation properties without rewriting drawing code. Updating the path in layoutSubviews also ensures the circle stays correct when Auto Layout changes the view size.
Fill the Circle or Draw a Ring
To draw a solid disk, give the layer a fill color and clear the stroke:
To draw a ring, keep the fill transparent and set lineWidth plus strokeColor. That covers common use cases such as badges, avatar outlines, and progress visuals.
The key design choice is whether the circle is a standalone shape layer or the entire view itself. If the whole view should be circular, a rounded layer may be even simpler.
When cornerRadius Is Enough
If the goal is just to make a view appear circular, do not over-engineer it. A square view with half-width corner radius becomes a circle:
This is perfect for image views, colored dots, or simple profile placeholders. It does not draw a separate vector path, but for many interface elements that is exactly what you want.
Use draw(_:) for Custom Rendering
If your view already draws multiple shapes, labels, or custom graphics, writing directly in draw(_:) can be the right choice:
This keeps rendering logic in one place, but it is more work to maintain than a shape layer. Use it when custom drawing is the point of the view, not when you only need one circle.
Make Sure the Drawing Area Is Square
One easy mistake is forgetting that ovalIn: draws the largest oval that fits the rectangle. If the rectangle is wider than it is tall, you get an ellipse, not a circle.
When the containing view is not square, compute a centered square drawing region:
That guarantees the geometry stays circular regardless of the view's aspect ratio.
Common Pitfalls
- Drawing into a non-square rectangle and accidentally producing an ellipse.
- Creating the path once in
initeven though the final view size is determined later by Auto Layout. - Reaching for
draw(_:)when a simpleCAShapeLayerwould be easier to style and animate. - Forgetting to set either a fill color or a stroke color, which can make the shape invisible.
- Using
cornerRadiuswithoutclipsToBoundswhen the view content should also be clipped.
Summary
- In UIKit, circles are usually drawn with
UIBezierPath(ovalIn:). - '
CAShapeLayeris the best default for most stroked or filled circle UI elements.' - '
cornerRadiusis the simplest option when the entire view should appear circular.' - Use
draw(_:)when the circle is part of broader custom rendering. - Always use a square drawing region if you need a true circle instead of an ellipse.

