设置父视图的clipsToBounds属性为true,子视图允许超出父视图布局,这时的子视图是无法点击的
子视图超出父视图
父视图重写hitTest方法,可以将事件传递到子视图
/// 超出视图外按钮的点击事件处理
///
/// - Parameters:
/// - point: 点击的点
/// - event: 事件
/// - Returns: 返回的按钮
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
var view = super.hitTest(point, with: event)
if view == nil {
for subView in self.subviews {
let p = subView.convert(point, from: self)
if subView.bounds.contains(p) {
view = subView
}
}
}
return view
}
网友评论