Rounded Borders in SwiftUI
Developers looking for circular shape & border.
Image("turtlerock")
.clipShape(Circle())
.overlay(
Circle().stroke(Color.white, lineWidth: 4))
That's not a workaround, it's how you do it in SwiftUI. Two things:
There used to be a
cornerRadius
modifier that became deprecated in... beta 4? beta 5? Yes, it's been a moving target.With a great amount of thanks to @kontiki (blog post), here's an extension that nicely returns what you want:
extension View { public func addBorder<S>(_ content: S, width: CGFloat = 1, cornerRadius: CGFloat) -> some View where S : ShapeStyle { let roundedRect = RoundedRectangle(cornerRadius: cornerRadius) return clipShape(roundedRect) .overlay(roundedRect.strokeBorder(content, lineWidth: width)) } }
Usage:
.addBorder(Color.white, width: 1, cornerRadius: 10)