.border() modifier always draws straight edges regardless of corner radius. To get a rounded border, apply .cornerRadius() to clip the background, then add an .overlay() with a RoundedRectangle(cornerRadius:).stroke() matching the same radius.Why this happens
SwiftUI’s .border(_:width:) modifier draws a rectangular stroke around the view’s frame. It does not respect .cornerRadius() because the border is rendered outside the view’s clipped bounds. When you chain .cornerRadius(20).border(.blue, width: 5), the view’s content and background are clipped to a rounded rectangle, but the border remains a sharp-cornered rectangle painted on top.
This behavior is by design: .border is a simple convenience for straight-edged outlines. For rounded outlines you must draw the stroke yourself using a shape that knows its own corner radius.
How to fix it
1. Overlay a stroked RoundedRectangle (recommended)
Apply the background and corner radius first, then overlay a RoundedRectangle with the same radius and stroke it. This keeps the border perfectly aligned with the clipped corners.
VStack {
Text("some text")
Button("Ok") {}
.foregroundColor(.cyan)
.padding()
}
.padding()
.background(.red)
.cornerRadius(20) // clips background & content
.overlay(
RoundedRectangle(cornerRadius: 20)
.stroke(.blue, lineWidth: 5) // draws rounded border
)
Key points:
– The cornerRadius value must match in both places.
– .stroke centers the stroke on the rectangle’s edge, so half the line width sits inside and half outside the clipped area. If you need the border fully outside, see variant 2.
2. Inset the overlay for pixel-perfect outer border
When the border must sit completely outside the rounded content (no half-width clipping), inset the rectangle by half the line width before stroking.
.overlay {
let lineWidth: CGFloat = 5
RoundedRectangle(cornerRadius: 20)
.inset(by: lineWidth / 2)
.stroke(.blue, lineWidth: lineWidth)
}
inset(by:) shrinks the rectangle so the stroke’s outer edge aligns with the original corner radius. Use this when the design spec calls for a border that does not encroach on the content area.
3. Reusable ViewModifier for consistent borders
If you need rounded borders throughout the app, wrap the pattern in a ViewModifier.
struct RoundedBorder: ViewModifier {
let color: Color
let width: CGFloat
let radius: CGFloat
func body(content: Content) -> some View {
content
.cornerRadius(radius)
.overlay(
RoundedRectangle(cornerRadius: radius)
.inset(by: width / 2)
.stroke(color, lineWidth: width)
)
}
}
extension View {
func roundedBorder(_ color: Color, width: CGFloat = 2, radius: CGFloat = 12) -> some View {
modifier(RoundedBorder(color: color, width: width, radius: radius))
}
}
// Usage
MyView()
.background(.red)
.roundedBorder(.blue, width: 5, radius: 20)
How to prevent it
- Never chain
.cornerRadius()directly before.border()expecting rounded corners. - Define a shared
ViewModifierorViewextension so the whole team uses the same rounded-border implementation. - When designing in Figma/Sketch, specify whether the border is centered, inside, or outside the corner radius so the implementation matches.
FAQ
Why does .border exist if it doesn’t support rounded corners?
.border is a lightweight shortcut for simple rectangular outlines (e.g., debug visualizations, wireframes). It intentionally avoids the layout complexity of shape-based strokes.
Can I animate the border color or width?
Yes. Because the overlay contains a standard RoundedRectangle with .stroke, you can animate color, lineWidth, or even cornerRadius inside a withAnimation block.
Does this work on macOS, watchOS, and visionOS too?
Yes. RoundedRectangle, .stroke, .overlay, and .cornerRadius are available on all Apple platforms that support SwiftUI.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.
