Draw line in UIView
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To draw a line in UIView, the classic UIKit approach is to subclass the view and implement draw(_:) with Core Graphics. That works well for static or lightweight custom drawing. For reusable divider lines or animated shapes, CAShapeLayer is often a cleaner alternative.
Draw a Line with Core Graphics
When you override draw(_:), UIKit gives you a drawing context for the view. You can move to a start point, add a line to an end point, and stroke the path.
This draws a horizontal line across the middle of the view. The line is redrawn whenever the system decides the view needs display, such as after layout or explicit invalidation.
Add the Custom View Like Any Other View
Once you have the subclass, use it like a normal UIView:
Setting the background color to clear is common when the line should appear without a solid view rectangle around it.
Prefer CAShapeLayer for Many Simple Lines
If you only need a divider or a line that may animate, CAShapeLayer can be more efficient and easier to configure than overriding draw(_:).
This approach separates the line from the drawing cycle and is often easier to animate or restyle.
Choose the Right Tool
Use draw(_:) when:
- You are already doing custom drawing in one place.
- The line is part of a more complex custom graphic.
- The view's content is naturally paint-based.
Use CAShapeLayer when:
- You want a simple, reusable line or shape.
- You may animate the stroke or path later.
- You want to avoid unnecessary redraw work.
Both are valid. The right choice depends on whether the line is just one graphic element or part of a larger custom-rendered view.
Avoid Excessive Redrawing
draw(_:) is not where you should put expensive calculations. The method may be called many times across the view lifecycle. If geometry can be precomputed or represented by a layer, that is often better. Also avoid calling setNeedsDisplay() repeatedly unless the visual content truly changed.
If the line depends on Auto Layout sizing, recompute the path from bounds instead of assuming the initial frame will remain valid forever.
Common Pitfalls
- Forgetting to call
strokePath()after building the line path. - Doing expensive logic inside
draw(_:). - Drawing with hardcoded coordinates that break after layout changes.
- Using
draw(_:)for a simple divider when a layer would be simpler. - Expecting the line to appear if the stroke color or line width was never configured.
Summary
- Override
draw(_:)and use Core Graphics for classic custom line drawing. - Use
CAShapeLayerfor lightweight reusable or animatable lines. - Recalculate line geometry when the view's bounds change.
- Keep
draw(_:)focused on drawing, not heavy logic. - Choose the simplest tool that matches how dynamic the line needs to be.
Related reading
- Draw text along circular path in Swift for iOS
- Drawing UIBezierPath on code generated UIView
- DSL element 'android.dataBinding.enabled' is obsolete and has been replaced with 'android.buildFeatures.dataBinding
- Duplicate class in Kotlin Android
- Duplicate class in Kotlin Android
- Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment
- Duplicate symbols for architecture x86_64 under Xcode
- dyld Library not loaded rpath with iOS8
.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.