On iOS, if a superview's userInteractionEnabled is NO, then all subviews are disabled as well?

nonopolarity picture nonopolarity · Jun 4, 2012 · Viewed 19.9k times · Source

I thought when a view is touched or tapped on, its handler get called first, and then its superview's handler is called (propagate upward).

But is it true that if the superview's userInteractionEnabled is set to NO, then all subviews and offspring is also disabled for user interaction? What if we want to disable for just the main view but don't want to disable for the subviews?

Answer

Patrick Pijnappel picture Patrick Pijnappel · Jun 29, 2016

You can override hitTest(_:withEvent:) to ignore the view itself, but still deliver touches to its subviews.

class ContainerStackView : UIStackView {
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let result = super.hitTest(point, with: event)
        if result == self { return nil }
        return result
    }
}