iosswiftuiipados

Disable view without disabling overlay


I have a view (modifier) whose body is akin to this:

content
    .disabled(isDisabled)
    .blur(radius: isDisabled ? 10 : 0)
    .overlay {
        Color.clear
            .disabled(!isDisabled)
            .highPriorityGesture(TapGesture().onEnded { _ in
                $isDisabled = false
            }, isEnabled: isDisabled)
    }

I'm expecting that when the view is disabled, the overlay will still allow me to tap on it to dismiss it, and therefore re-enable the view. However, disabling the view also disables the overlay, even though the overlay attempts to explicitly re-enable itself. I've also attempted to do this with allowsHitTesting with the same result. How can I achieve what I'm trying to do here? Assume that re-arranging the view hierarchy around this is non-trivial


Solution

  • Try giving the Color.clear a .contentShape, to make it receptive to taps. You might also want to make the overlay conditional on the content being disabled:

    content
        .disabled(isDisabled)
        .blur(radius: isDisabled ? 10 : 0)
        .overlay {
            if isDisabled {
                Color.clear
                    .contentShape(Rectangle())
                    .onTapGesture {
                        isDisabled = false
                    }
            }
        }