SwiftUI — set up different corner radius of view in SwiftUI

ODENZA
May 16, 2021

--

In this article, I will show you how to custom set up different corner radius of view in SwiftUI.

extension view…

extension View {   func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {      clipShape( RoundedCorner(radius: radius, corners: corners) )   }
}

and

struct RoundedCorner: Shape {var radius: CGFloat = .infinity
var corners: UIRectCorner = .allCorners
func path(in rect: CGRect) -> Path {let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))return Path(path.cgPath)}}

How to use…

Rectangle()
.fill(Color.red)
.frame(width: 100, height: 100)
.cornerRadius(6, corners: [.topLeft, .topRight])

--

--

ODENZA
ODENZA

No responses yet