Fix: Button Border Corners Cut Off with cornerRadius in SwiftUI

Fix: Button Border Corners Cut Off with cornerRadius in SwiftUI

Written by

in

Quick answer: The .border() modifier draws a rectangle that doesn’t respect .cornerRadius() applied after it. Fix it by replacing .border() with an .overlay() containing a RoundedRectangle(cornerRadius:).stroke() and apply .cornerRadius() to any background color as well.

Why this happens

SwiftUI’s .border() modifier draws a simple rectangular stroke around the view’s frame. When you chain .cornerRadius() after .border(), the corner radius only clips the view’s content and background — the border itself remains a sharp rectangle. The result is a visible mismatch: the button’s fill rounds correctly, but the border stays square and gets clipped at the corners, creating the “cut-off” appearance.

The modifier order matters. In View.border().cornerRadius(), the border is rendered first as a rectangle, then the corner radius masks the combined result. Since the border sits outside the content bounds, the mask cuts through it. This behavior is consistent across iOS, macOS, and visionOS.

How to fix it

1. Use overlay with RoundedRectangle stroke (recommended)

Replace .border() with an .overlay() that draws a RoundedRectangle stroke matching your desired corner radius. This keeps the border inside the rounded bounds.

Button(action: {
    print("Sign up tapped")
}) {
    Text("SIGN UP")
        .frame(maxWidth: .infinity)
        .font(.system(size: 18, weight: .semibold))
        .padding()
        .foregroundColor(.white)
        .overlay(
            RoundedRectangle(cornerRadius: 25)
                .stroke(Color.white, lineWidth: 2)
        )
}
.background(Color.blue) // optional background
.cornerRadius(25)       // match the overlay's radius

Key points:
– The RoundedRectangle(cornerRadius: 25) in the overlay matches the .cornerRadius(25) on the background.
– lineWidth: 2 replaces the width: 2 from .border().
– Apply .cornerRadius() to the background (or the button itself if no background) so the fill rounds correctly.

2. Create a reusable ButtonStyle for consistent buttons

For apps with many bordered buttons, encapsulate the logic in a ButtonStyle. This also handles pressed/disabled states automatically.

struct BorderedButtonStyle: ButtonStyle {
    var backgroundColor: Color = .blue
    var foregroundColor: Color = .white
    var borderColor: Color = .white
    var cornerRadius: CGFloat = 12
    var lineWidth: CGFloat = 2
    var isDisabled: Bool = false

    func makeBody(configuration: Configuration) -> some View {
        let pressed = configuration.isPressed
        let dimmed = isDisabled || pressed
        let bg = backgroundColor.opacity(dimmed ? 0.6 : 1.0)
        let fg = foregroundColor.opacity(dimmed ? 0.6 : 1.0)
        let border = borderColor.opacity(dimmed ? 0.6 : 1.0)

        configuration.label
            .font(.system(size: 17, weight: .semibold))
            .foregroundColor(fg)
            .frame(maxWidth: .infinity)
            .padding(.vertical, 14)
            .padding(.horizontal, 20)
            .background(bg)
            .cornerRadius(cornerRadius)
            .overlay(
                RoundedRectangle(cornerRadius: cornerRadius)
                    .stroke(border, lineWidth: lineWidth)
            )
            .scaleEffect(pressed ? 0.98 : 1.0)
            .animation(.easeOut(duration: 0.1), value: pressed)
    }
}

// Usage
Button("Continue") { }
    .buttonStyle(BorderedButtonStyle())

Button("Delete") { }
    .buttonStyle(BorderedButtonStyle(
        backgroundColor: .red,
        borderColor: .red,
        foregroundColor: .white
    ))

This style:
– Keeps border and background radii in sync via a single cornerRadius parameter.
– Dims colors on press/disable for native feel.
– Adds a subtle scale animation on press.
– Works with any label (text, icon, HStack).

3. Quick fix for existing code using .border()

If you cannot refactor to overlay immediately, move .cornerRadius() before .border() and add a tiny padding so the border sits outside the clipped area:

// Works but border sits outside the visual bounds
Button("Sign Up") { }
    .padding()
    .background(Color.blue)
    .cornerRadius(25)
    .padding(2)           // extra space for border
    .border(Color.white, width: 2)

This is a workaround — the border will appear outside the rounded background. Prefer Fix 1 for pixel-perfect results.

How to prevent it

  • Always pair a rounded background with an overlay stroke of the same cornerRadius.
  • Treat .border() as a rectangular stroke only; never expect it to round corners.
  • Build a shared ButtonStyle or ViewModifier early in the project to enforce consistency.
  • Test buttons in both light and dark mode — border colors often need semantic colors (Color.primary, Color(uiColor: .systemBlue)) to stay visible.
  • Verify pressed/disabled states in Previews with .environment(\ .isEnabled, false) and manual press simulation.

FAQ

Why does .border().cornerRadius() clip the border instead of rounding it?

.border() draws a rectangle around the view’s current frame. The subsequent .cornerRadius() applies a clipping mask to the entire view (content + border). Since the border extends to the frame’s sharp corners, the mask cuts through it. The border itself never becomes rounded.

Can I use .clipShape(RoundedRectangle(...)) instead of .cornerRadius()?

Yes. .clipShape(RoundedRectangle(cornerRadius: 25)) behaves identically to .cornerRadius(25) for this problem — it still clips a rectangular border. The fix remains the same: use an overlay stroke with a matching RoundedRectangle.

Does this apply to other views like TextField or custom shapes?

Yes. Any view where you apply .border() then .cornerRadius() (or .clipShape()) will show the same cut-off effect. The overlay-stroke pattern works universally: TextField(...).overlay(RoundedRectangle(cornerRadius: 8).stroke(Color.gray)).

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.