Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: How to apply .clipShape to .hoverEffect?

I’m using the .hoverEffect modifier. It adds an hover effect to my button when users have their mouse/trackpad pointer on it:

import SwiftUI

struct ContentView: View {
    var body: some View {
        Button("Hello world") {
            // Action…
        }
        .padding()
        .background(Color.blue)
        .foregroundColor(.white)
        .hoverEffect(.lift) // ← Hover effect
        .clipShape(Capsule())
    }
}

However, when I apply a clip shape to the button (e.g. RoundedRectangle or Capsule), the view is clipped but the hover effect isn’t.

Button with a hover effect

How can I have a hover effect that matches the shape of the button?

like image 760
Nicolapps Avatar asked Dec 06 '25 05:12

Nicolapps


2 Answers

Use .contentShape(Capsule()) to change the shape of the hover effect to match your clipShape. Note that this modifier also affects the hit testing area, which is probably desirable in most examples similar to your own.

like image 186
MutatingFunc Avatar answered Dec 10 '25 19:12

MutatingFunc


In SwiftUI, the order of modifier can change the view's behaviour, if you want to solve this problem, make sure the .hoverEffect(:_) used after clipShape.

use the code below instead:

// declarations...
.clipShape(Capsule())
.hoverEffect(.lift)
like image 32
Akivili Collindort Avatar answered Dec 10 '25 21:12

Akivili Collindort



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!