UIView. Why Does A Subviews Outside its Parent's Extent Not Receive Touches?

后端 未结 5 1162
盖世英雄少女心
盖世英雄少女心 2020-12-04 19:29

I have a simple - trivial - UIView parent/child hierarchy. One parent (UIView). One child (UIButton). The parents bounds are smaller then it\'s child\'s bounds so that a por

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 20:12

    Precondition:

    You have a UIButton(named as button1) inside a UIView(named as container), and button1 is partially outside the container's bounds.

    Problem:

    the part outside the container of button1 will not response click.

    Solution:

    subclass your container from UIView:

    class Container: UIView {
        override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
    
            let closeButton = viewWithTag(10086) as! UIButton  //<-- note the tag value
            if closeButton.pointInside(convertPoint(point, toView: closeButton), withEvent: event) {
                return true
            }
            return super.pointInside(point, withEvent: event)
        }
    }
    

    Don't forget to give your button1 a tag of 10086

提交回复
热议问题