Friday, August 19, 2022
HomeiOS Developmentios - SwiftUI: Forestall .onHover on overlapping views

ios – SwiftUI: Forestall .onHover on overlapping views

I’ve two views that each react to .onHover overlapping in a ZStack. Presently the impact is triggered within the blue view even whether it is overlapped by the pink view (see video). How can I be sure that solely the view on the high of the ZStack will react to .onHover, in order that the blue view is not going to change when the overlapping a part of the pink view is hovered?

enter image description here


struct ContentView: View {
    var physique: some View {
        ZStack {
            RectangleView(shade: .blue)
                .body(width: 300, peak: 300)
            RectangleView(shade: .pink)
                .body(width: 150, peak: 150)
                .offset(x: -150)
        .body(width: 600, peak: 400)

struct RectangleView: View {
    @State var hover = false
    var shade: Shade

    var physique: some View {
            .overlay(hover ? Shade.white.opacity(0.3) : Shade.clear)
            .onHover { isHovering in
                if isHovering {
                    hover = true
                } else {
                    hover = false



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments