Rounded Borders in SwiftUI

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)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)