本文主要是介绍UIButton超出UIView边界不可点击,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
当按钮超过了父视图范围,点击是没有反应的。因为消息的传递是从最下层的父视图开始调用hittest方法。
- (UIView )hitTest:(CGPoint)point withEvent:(UIEvent )event {
UIView *view = [super hitTest:point withEvent:event];
return view;
}
当存在view时才会传递对应的event,现在点击了父视图以外的范围,自然返回的是nil。所以当子视图(比如按钮btn)因为一些原因超出了父视图范围,就要重写hittest方法,让其返回对应的子视图,来接收事件。
- (UIView )hitTest:(CGPoint)point withEvent:(UIEvent )event {
UIView *view = [super hitTest:point withEvent:event];
if (view == nil) {
CGPoint tempoint = [btn convertPoint:point fromView:self];
if (CGRectContainsPoint(btn.bounds, tempoint))
{
view = btn;
}
}
return view;
}
或
- (UIView )hitTest:(CGPoint)point withEvent:(UIEvent )event { UIView *result = [super hitTest:point withEvent:event]; CGPoint buttonPoint = [self.button convertPoint:point fromView:self]; if ([self.button pointInside:buttonPoint withEvent:event]) { return self.button; } return result; }
这篇关于UIButton超出UIView边界不可点击的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!