Fix: Round Specific Corners of a View in SwiftUI

Written by

in

Quick answer: On iOS 16+, use .clipShape(.rect(topLeadingRadius: 0, bottomLeadingRadius: 20, bottomTrailingRadius: 0, topTrailingRadius: 20)) with UnevenRoundedRectangle. For iOS 13–15, add a View extension that clips with a custom RoundedCorner shape using UIRectCorner.

Why this happens

SwiftUI’s built-in .cornerRadius() modifier applies the same radius to all four corners. There is no parameter to select individual corners, so developers who need only the top or bottom corners rounded must provide their own clipping shape. Apple introduced a first-party solution in iOS 16 with UnevenRoundedRectangle, but projects targeting older iOS versions still need a custom Shape implementation.

How to fix it

1. iOS 16 and later — use the native UnevenRoundedRectangle

Starting with iOS 16 (Xcode 15 required), SwiftUI ships UnevenRoundedRectangle via the .rect shape style. Pass the four radii you want; zero leaves a corner sharp.

import SwiftUI

struct TopRoundedCard: View {
    var body: some View {
        Text("Rounded top corners only")
            .frame(maxWidth: .infinity, minHeight: 200)
            .background(Color.blue)
            .clipShape(
                .rect(
                    topLeadingRadius: 24,
                    bottomLeadingRadius: 0,
                    bottomTrailingRadius: 0,
                    topTrailingRadius: 24
                )
            )
    }
}

Use .clipShape (or .background with the same shape) so the clipping respects the view’s actual size.

2. iOS 13 through 15 — custom RoundedCorner shape

Create a Shape that wraps UIBezierPath(roundedRect:byRoundingCorners:cornerRadii:) and expose it through a View extension.

import SwiftUI

struct RoundedCorner: Shape {
    let radius: CGFloat
    let corners: UIRectCorner

    init(radius: CGFloat = .infinity, corners: UIRectCorner = .allCorners) {
        self.radius = radius
        self.corners = corners
    }

    func path(in rect: CGRect) -> Path {
        let path = UIBezierPath(
            roundedRect: rect,
            byRoundingCorners: corners,
            cornerRadii: CGSize(width: radius, height: radius)
        )
        return Path(path.cgPath)
    }
}

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

Usage:

VStack {
    Text("Bottom corners rounded")
        .frame(maxWidth: .infinity, minHeight: 150)
        .background(Color.green)
        .cornerRadius(20, corners: [.bottomLeft, .bottomRight])
}

3. Alternative — Path + GeometryReader (no UIKit dependency)

If you prefer pure SwiftUI without UIBezierPath, build the path manually. This approach also works on macOS where UIRectCorner is unavailable.

struct UnevenRoundedShape: Shape {
    var topLeading: CGFloat = 0
    var topTrailing: CGFloat = 0
    var bottomLeading: CGFloat = 0
    var bottomTrailing: CGFloat = 0

    func path(in rect: CGRect) -> Path {
        var path = Path()
        let w = rect.width
        let h = rect.height
        // Clamp radii so arcs never exceed half the side length
        let tl = min(topLeading, min(w, h) / 2)
        let tr = min(topTrailing, min(w, h) / 2)
        let bl = min(bottomLeading, min(w, h) / 2)
        let br = min(bottomTrailing, min(w, h) / 2)

        path.move(to: CGPoint(x: w / 2, y: 0))
        path.addLine(to: CGPoint(x: w - tr, y: 0))
        path.addArc(center: CGPoint(x: w - tr, y: tr),
                    radius: tr,
                    startAngle: .degrees(-90),
                    endAngle: .degrees(0),
                    clockwise: false)
        path.addLine(to: CGPoint(x: w, y: h - br))
        path.addArc(center: CGPoint(x: w - br, y: h - br),
                    radius: br,
                    startAngle: .degrees(0),
                    endAngle: .degrees(90),
                    clockwise: false)
        path.addLine(to: CGPoint(x: bl, y: h))
        path.addArc(center: CGPoint(x: bl, y: h - bl),
                    radius: bl,
                    startAngle: .degrees(90),
                    endAngle: .degrees(180),
                    clockwise: false)
        path.addLine(to: CGPoint(x: 0, y: tl))
        path.addArc(center: CGPoint(x: tl, y: tl),
                    radius: tl,
                    startAngle: .degrees(180),
                    endAngle: .degrees(270),
                    clockwise: false)
        path.closeSubpath()
        return path
    }
}

// Convenience extension
 extension View {
    func clipUnevenCorners(
        topLeading: CGFloat = 0,
        topTrailing: CGFloat = 0,
        bottomLeading: CGFloat = 0,
        bottomTrailing: CGFloat = 0
    ) -> some View {
        clipShape(UnevenRoundedShape(
            topLeading: topLeading,
            topTrailing: topTrailing,
            bottomLeading: bottomLeading,
            bottomTrailing: bottomTrailing
        ))
    }
 }

Apply it like:

Image("hero")
    .resizable()
    .aspectRatio(contentMode: .fill)
    .frame(height: 250)
    .clipUnevenCorners(topLeading: 30, topTrailing: 30)

How to prevent it

  • Adopt the native UnevenRoundedRectangle as soon as your minimum deployment target reaches iOS 16.
  • Keep the custom RoundedCorner shape in a shared UI kit so legacy targets stay consistent.
  • Clamp radii to half the view’s shortest side (as shown in the pure-SwiftUI shape) to avoid malformed paths when the view is smaller than the radius.
  • Prefer .clipShape over .background(shape) when the content itself must be clipped (e.g., images, scroll views).

FAQ

Does UnevenRoundedRectangle work on macOS?

Yes. The .rect shape style and UnevenRoundedRectangle are available on macOS 13+, iOS 16+, tvOS 16+, and watchOS 9+.

Can I animate corner-radius changes?

Both the native shape and the custom Shape conform to Animatable. Wrap radius changes in withAnimation and the corners will interpolate smoothly.

Why does my custom shape clip the view’s shadow?

.clipShape clips the entire layer, including shadows. Apply .shadow() after the clip modifier, or use .background(shape.shadow(...)) to keep the shadow outside the clipped bounds.{

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.