How to make a dashed line in swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Swift, a dashed line is usually drawn either with Core Graphics in draw(_:) or with a CAShapeLayer. Both approaches work, but CAShapeLayer is often the cleaner choice for reusable UI because it integrates well with layers, animations, and Auto Layout updates. The key property in both approaches is the dash pattern itself: alternating dash and gap lengths.
Use CAShapeLayer for a Reusable Dashed Line
A CAShapeLayer is a good default for UIKit views because it draws efficiently and is easy to restyle.
The lineDashPattern array means "draw 6 points, skip 4 points, repeat."
Why layoutSubviews Matters
A common bug is creating the dashed path once in init and then wondering why the line is wrong after Auto Layout changes the view size. The line path depends on bounds, so it should usually be updated in layoutSubviews().
That keeps the dashed line aligned with the view's actual size after constraints are resolved.
Draw With Core Graphics When Custom Drawing Fits Better
If you already have a custom drawing view, Core Graphics is also a solid option.
This is fine when the dashed line is part of a larger custom-drawn surface.
Vertical or Custom Paths
The same idea works for vertical lines or more complex shapes. Only the path changes.
You can also apply a dashed stroke to rectangles, dividers, borders, or more complex UIBezierPath shapes.
SwiftUI Note
If you are working in SwiftUI instead of UIKit, use a Shape or built-in stroke styling rather than a CAShapeLayer.
That is the SwiftUI-native approach and avoids mixing UIKit drawing patterns into a SwiftUI view tree.
Common Pitfalls
The most common mistake is drawing the path once and never updating it after the view's size changes.
Another issue is forgetting to set fillColor to nil on a CAShapeLayer when you only want a dashed stroke.
Developers also sometimes put the dashed layer creation in layoutSubviews() without guarding against duplicates, which keeps adding more sublayers every layout pass.
Finally, choose the drawing approach that matches the framework. CAShapeLayer fits UIKit well, while stroke styling is usually cleaner in SwiftUI.
Summary
- Use
CAShapeLayerfor a clean reusable UIKit dashed line. - Update the path in
layoutSubviews()so it matches the final view size. - Use Core Graphics when the dashed line is part of custom drawing code.
- In SwiftUI, use stroke styling with a dash pattern.
- The dash effect always comes from alternating dash and gap lengths in the stroke configuration.
Related reading
- How to make a drop shadow effect on a label in Swift?
- How to make a edittext box in a dialog
- How to make a phone call using intent in Android?
- How to make a random color with Swift
- How to make a simple collection view with Swift
- How to make a simple collection view with Swift
- How to make a simple rounded button in Storyboard?
- How to make a smooth image rotation in Android?
.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.