Skip to content

Instantly share code, notes, and snippets.

@sebj
Created November 3, 2022 12:02
Show Gist options
  • Save sebj/3b32e37364e57739570fd713a4f6bfbb to your computer and use it in GitHub Desktop.
Save sebj/3b32e37364e57739570fd713a4f6bfbb to your computer and use it in GitHub Desktop.
Round specific corners of a view/shape
extension View {
/// Clips this view to its bounding frame, with the specified corner radius for the given corners.
///
/// By default, a view's bounding frame only affects its layout, so any
/// content that extends beyond the edges of the frame remains visible. Use
/// `cornerRadius(_:corners:)` to hide any content that extends beyond
/// these edges while applying a corner radius.
///
/// - Returns: A view that clips this view to its bounding frame with the
/// specified corner radius applied to the given corners.
@warn_unqualified_access
@ViewBuilder
func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
clipShape(ConfigurableRoundedRectangle(radius: radius, corners: corners))
}
}
struct ConfigurableRoundedRectangle: Shape {
let radius: CGFloat
let corners: UIRectCorner
func path(in rect: CGRect) -> Path {
let path = UIBezierPath(
roundedRect: rect,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius)
)
return Path(path.cgPath)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment