Skip to content

Instantly share code, notes, and snippets.

@jimmyhoran
Created July 9, 2021 06:21
Show Gist options
  • Save jimmyhoran/6de267e9c9fe409ec1eaf1d7cc4f8df8 to your computer and use it in GitHub Desktop.
Save jimmyhoran/6de267e9c9fe409ec1eaf1d7cc4f8df8 to your computer and use it in GitHub Desktop.
SwiftUI `View` extension to set the corner radius for each individual corner i.e. top corners only.
import SwiftUI
import UIKit
extension View {
func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
clipShape(RoundedCorner(radius: radius, corners: corners))
}
}
private 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 .init(path.cgPath)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment