In SwiftUI, why does a thick stroke on a Circle spill past its frame, and how do strokeBorder, trim and StrokeStyle build an activity ring?
answer
- centred on the path
- half the width outside
- InsettableShape only
- trim takes fractions
- default cap is butt
basics
~20 sstroke centres its line on the path, so half the width falls outside the frame; strokeBorder insets first and stays inside. A ring adds trim(from:to:) for progress and a round-capped StrokeStyle, insetting by hand because trim drops strokeBorder.
solid answer
~40 s`stroke(lineWidth:)` draws a line centred on the path, so a 20-point stroke on a circle in a 140-point frame reaches 10 points past the frame and can overlap neighbours or be clipped. `strokeBorder(lineWidth:)`, available on `InsettableShape` types such as `Circle` and `RoundedRectangle`, insets the shape by half the line width and then strokes it, so the line stays inside. For a ring I draw a faint `strokeBorder` track and, on top, `Circle().inset(by: lineWidth / 2).trim(from: 0, to: progress).stroke(style:)`: `trim` returns a plain `some Shape` without `strokeBorder`, so I inset by hand. `StrokeStyle(lineWidth:lineCap: .round)` softens the ends (the default cap is `.butt`), and `rotationEffect(.degrees(-90))` moves the start from 3 o'clock to 12.
code
swift · 22 linesimport SwiftUI
struct ActivityRing: View {
var progress: Double // 0...1, already clamped by the caller
var lineWidth: CGFloat = 20
var body: some View {
ZStack {
Circle()
.strokeBorder(.green.opacity(0.25), lineWidth: lineWidth)
Circle()
.inset(by: lineWidth / 2)
.trim(from: 0, to: progress)
.stroke(
AngularGradient(colors: [.green, .mint], center: .center),
style: StrokeStyle(lineWidth: lineWidth, lineCap: .round)
)
.rotationEffect(.degrees(-90))
}
.frame(width: 140, height: 140)
}
}go deeper
Recall that stroke is centred on the outline while strokeBorder stays inside it, and that trim takes fractions from 0 to 1.
Explain why strokeBorder needs an InsettableShape, why trim loses it, and how inset(by:) restores the same track. Know the StrokeStyle defaults, especially the butt cap.
Show you build rings and gauges that keep layout honest, handle clamping and start angles, and know the iOS 17 fill-and-stroke chaining and its deployment-target limit.
Treat ring and gauge drawing as a shared component: one implementation of track, progress, caps and gradients that every screen reuses instead of re-deriving the geometry.
## Where a stroke lands `stroke(lineWidth:)` and `stroke(_:style:)` trace a line **centred on the shape's path**. Half the line width falls inside the outline and half outside. For a `Circle` in a 140-point frame with `lineWidth: 20`, the circle's path runs along the frame edge, so the outer 10 points of the stroke land **outside the frame**. SwiftUI does not clip drawing to a view's frame by default, so the overflow is usually visible: it overlaps neighbouring views, or a clipping container such as a scroll view or a `clipShape` cuts it off. Layout still treats the view as 140 points, so spacing around it looks wrong. ## strokeBorder and InsettableShape `strokeBorder(_:lineWidth:)` exists only on `InsettableShape`, the protocol adopted by `Circle`, `Capsule`, `Ellipse`, `Rectangle` and `RoundedRectangle`. The older inlinable overload in the SDK interface shows the mechanism: it calls `inset(by: style.lineWidth * 0.5)` and then strokes the inset shape. The result is a line whose **outer edge** touches the frame, so the whole stroke stays inside. | Modifier | Available on | Where the line sits | |---|---|---| | `stroke(lineWidth:)` | every `Shape` | centred on the path, half outside the frame | | `strokeBorder(lineWidth:)` | `InsettableShape` only | fully inside the frame | | `inset(by:)` then `stroke` | `InsettableShape` only | wherever you inset it to | ## Drawing progress with trim `trim(from:to:)` keeps a fraction of a shape's path: both arguments are **fractions from 0 to 1**, not angles. A `Circle`'s path starts at its rightmost point, 3 o'clock, and runs clockwise on screen, so `trim(from: 0, to: 0.25)` is the quarter from 3 to 6 o'clock. A ring that starts at 12 o'clock adds `rotationEffect(.degrees(-90))`. The catch: `trim` returns a plain `some Shape`, which is **not** an `InsettableShape`, so `strokeBorder` is no longer available. The compiler says so: ```plaintext error: value of type 'some Shape' has no member 'strokeBorder' ``` Calling `strokeBorder` first does not help either, because it returns a view, which has no `trim`. The fix is to inset by hand before trimming: `Circle().inset(by: lineWidth / 2).trim(from: 0, to: progress).stroke(style:)`. That keeps the progress arc on exactly the same track as a `strokeBorder` background ring of the same width. ## StrokeStyle options `StrokeStyle(lineWidth:lineCap:lineJoin:miterLimit:dash:dashPhase:)` carries every stroke setting. The defaults matter for rings: - `lineWidth` defaults to 1; - `lineCap` defaults to `.butt`, a square cut flush with the end of the path; `.round` gives the soft ends people expect on an activity ring, and `.square` extends a square cap past the end; - `lineJoin` defaults to `.miter` with a `miterLimit` of 10; - `dash` defaults to an empty array (solid) and `dashPhase` to 0; a dashed track is `dash: [4, 6]`. A round cap extends half the line width past each end of the trimmed path, so a short arc looks slightly longer than its fraction. ## Gradients on the ring A stroke takes any `ShapeStyle`. `AngularGradient(colors:center:)` sweeps colours around the centre, which suits a ring; `LinearGradient` and `RadialGradient` suit straight bars and glows. Because the rotation is applied to the view after styling, `rotationEffect` turns the gradient together with the arc, so the colour change follows the progress. ## Fill and stroke on one shape From **iOS 17**, `fill(_:style:)` and `stroke(_:lineWidth:)` return types that conform to `ShapeView` and keep the shape, so `Circle().fill(.yellow).stroke(.blue, lineWidth: 8)` draws both. With a deployment target of iOS 16 or earlier the compiler rejects that chain, and you layer a filled shape with an overlay that strokes a second copy. ## Common mistakes - Stroking a full-radius circle and then wondering why the ring overlaps the label below it. - Passing a percentage such as 70 to `trim` instead of the fraction 0.7. - Giving the track and the progress arc different line widths or insets, so the two no longer sit on the same circle. - Rotating the whole `ZStack`, which also turns any label placed inside the ring; rotate only the progress arc. ## Checklist for a ring 1. Draw the track with `strokeBorder` at a low opacity. 2. Draw progress with `inset(by: lineWidth / 2)`, then `trim`, then `stroke(style:)`. 3. Use `lineCap: .round` and rotate by -90 degrees to start at 12 o'clock. 4. Clamp progress to 0...1 before passing it in; `trim` expects fractions.
- How do you give one shape both a fill and an outline?From iOS 17, `fill(_:style:)` and `stroke(_:lineWidth:)` return shape views that keep the underlying shape, so `Circle().fill(.yellow).stroke(.blue, lineWidth: 8)` draws both. With a deployment target of iOS 16 or earlier that chain does not compile; you layer a filled shape with an `overlay` containing a stroked copy of the same shape.
- Why does a trimmed ring start at 3 o'clock, and how do you move the start?A `Circle`'s path begins at its rightmost point and runs clockwise on screen, so `trim(from: 0, to: 0.25)` covers 3 o'clock to 6 o'clock. Applying `rotationEffect(.degrees(-90))` to the trimmed view starts it at 12 o'clock; because the rotation applies to the styled view, an `AngularGradient` used as the stroke style turns with it.
- When would you use a dashed StrokeStyle on a ring?For a segmented look such as one dash per day of a weekly goal. `StrokeStyle(lineWidth: 12, dash: [8, 4])` draws 8-point dashes with 4-point gaps along the path, and `dashPhase` shifts where the pattern starts. Dash lengths are in points along the path, so for exact segments compute them from the circumference.
- strokeBorder works on any Shape, including a trimmed one
- A stroke is drawn entirely inside the shape's frame
- trim(from:to:) takes start and end angles in degrees
- StrokeStyle rounds line ends unless told otherwise
- Calling strokeBorder before trim fixes the compile error