Fix: Fill and Stroke a Shape at the Same Time in SwiftUI

Written by

in

Quick answer: In iOS 17+, chain .stroke() then .fill() on a shape. In iOS 16, use .strokeBorder() with a .background() containing the same shape filled. Both produce a single view with a fill color and a border.

Why this happens

SwiftUI shapes like Circle, Rectangle, and RoundedRectangle are value types that conform to the Shape protocol. When you apply .fill() or .stroke(), SwiftUI wraps the shape in a ShapeView that locks in the rendering style. The return type of .fill() is ShapeView<Shape, Color> (or similar), which does not expose a .stroke() method, and vice‑versa. This is why Circle().fill(Color.red).stroke(Color.blue) fails with “Value of type ‘ShapeView<…>’ has no member ‘stroke’”.

In UIKit you draw into a CGContext where fill and stroke are separate drawing operations on the same path. SwiftUI’s declarative pipeline separates them into distinct view modifiers, so you must combine two shape views or, on newer OS versions, use the new chaining API.

How to fix it

1. iOS 17 and later – chain stroke then fill

Starting with iOS 17 / macOS 14 / watchOS 10 / tvOS 17, SwiftUI allows you to stack .stroke() and .fill() directly on the same shape. The order matters: call .stroke() first, then .fill().

import SwiftUI

struct FilledAndStrokedCircle: View {
    var body: some View {
        Circle()
            .stroke(.blue, lineWidth: 4)   // border
            .fill(.red)                    // interior
            .frame(width: 150, height: 150)
    }
}

The modifier chain produces a single view that renders both the stroke and the fill. You can swap the colors or use gradients, Material, or any ShapeStyle.

2. iOS 16 and earlier – strokeBorder + background

On older systems the accepted workaround is to draw the border with .strokeBorder() (which insets the stroke so it stays inside the shape bounds) and place a filled copy of the same shape behind it using .background().

struct FilledAndStrokedCircleLegacy: View {
    var body: some View {
        Circle()
            .strokeBorder(Color.blue, lineWidth: 4)
            .background(Circle().foregroundColor(Color.red))
            .frame(width: 150, height: 150)
    }
}

strokeBorder ensures the stroke does not exceed the layout bounds, and background layers the filled shape underneath. This compiles on all SwiftUI versions.

3. Reusable ViewModifier for cross‑version code

If you support both iOS 16 and 17, wrap the logic in a modifier that picks the best implementation at runtime.

struct FilledAndStroked: ViewModifier {
    let fill: Color
    let stroke: Color
    let lineWidth: CGFloat

    func body(content: Content) -> some View {
        if #available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, *) {
            content
                .stroke(stroke, lineWidth: lineWidth)
                .fill(fill)
        } else {
            content
                .strokeBorder(stroke, lineWidth: lineWidth)
                .background(content.foregroundColor(fill))
        }
    }
}

extension Shape {
    func filledAndStroked(fill: Color, stroke: Color, lineWidth: CGFloat = 1) -> some View {
        modifier(FilledAndStroked(fill: fill, stroke: stroke, lineWidth: lineWidth))
    }
}

// Usage
Circle().filledAndStroked(fill: .red, stroke: .blue, lineWidth: 4)

This keeps call sites clean and guarantees the correct rendering path for each deployment target.

How to prevent it

  • Target iOS 17+ when possible to use the simpler chaining syntax.
  • Prefer strokeBorder over stroke when you need the border to stay inside the shape’s frame.
  • Encapsulate the version check in a reusable modifier or view so you don’t repeat the workaround.
  • Test on the minimum deployment target; the background approach can produce a one‑pixel fringe if the shape uses an odd line width on non‑retina screens.

FAQ

Does the chaining order matter in iOS 17?

Yes. Call .stroke() first, then .fill(). Reversing them compiles but the fill will cover the stroke, leaving only the fill visible.

Can I use gradients or materials instead of solid colors?

Absolutely. Both .fill() and .stroke() accept any ShapeStyle, so you can pass LinearGradient, AngularGradient, .thinMaterial, etc.

Why not just ZStack two shapes?

A ZStack works but creates two separate layout passes and two view nodes. The strokeBorder + background or the iOS 17 chaining produce a single composed view that is more efficient and easier to animate as a unit.{

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.