UIButton无法在UIScrollView的初始帧外部单击 [英] UIButton not clickable outside initial frame of an UIScrollView

查看:197
本文介绍了UIButton无法在UIScrollView的初始帧外部单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了UIScrollView的奇怪行为,在放大UIScrollView的contentSize之后我无法点击UIButton。

I got stuck with the strange behavior of UIScrollView that I cannot click on the UIButton after I enlarge the contentSize of the UIScrollView.

我想做的事:


  1. 使用UIScrollView显示表单

点击提交按钮后,滚动视图将放大一些高度以显示更多信息。

在进一步的信息中,我将放置并显示另一个UIButton(隐藏在开头),以便处理下一步。

问题是我将新UIButton放到放大区域并且按钮不可点击而我将UIButton放置到现有区域(滚动视图的初始帧)然后按钮工作正常。对于这两种情况,滚动视图的滚动条执行正常行为。(即,滚动结束是新的内容高度)

The problem is that I placed the new UIButton to the enlarged area and the button is not clickable whereas I placed the UIButton to existing area(the initial frame of scroll view) then the button works normally. For both cases, the scroll bar of the scroll view performs normal behavior.(ie, the scroll end is the new content height)

这是我到目前为止所拥有的:

Here is what I have so far:


  • 一个UIView xib(放置其中的所有元素,包括隐藏按钮)

  • A UIView xib (placed all elements inside it including the hidden button)

UIScrollView(将UIView xib加载到其中)

A UIScrollView (loaded the UIView xib into it)


UIView * view = [[[NSBundle mainBundle] loadNibNamed:@viewowner:self options:nil] objectAtIndex:0];

[view loaded];

[scrollView addSubview:view];


  • 点击提交按钮后:

// offset =一些金额;

[scrollview setContentSize:CGSizeMake(scrollview.contentSize.width,scrollview.contentSize.height + offset)];

// h =滚动视图结束前的一些金额

CGRect r = nextBt n.frame;

r.origin.y = h;

nextBtn.frame = r;

[nextBtn setHidden:NO];

我试图更改滚动视图的clipSubviews属性,但似乎根本不适用于我的情况。

I have tried to change the clipSubviews attribute of scroll view but seems it is not working for my case at all.

任何人都知道它是如何发生的?有没有更好的方法来完成这项工作? (调整滚动视图的大小,然后在其中放置另一个按钮)

Anyone knows how it happens? And is there any better methods to get this job done? (resize the scroll view and then place another button in it)

任何帮助都会很棒!非常感谢!

Any help would be great! Thanks a lot!

推荐答案

我假设该按钮是您从xib文件加载的内容视图的直接或间接子项。

I assume that the button is direct or indirect child of the contents view you load from the xib file.

问题是因为您更改了滚动视图的 contentSize ,但没有更改内容视图的框架。超级视图参与触摸测试。您可以将子视图放在其超视图范围之外,默认情况下,子视图不会被剪裁,但触摸事件将不会传递给它,因为超级视图在命中测试期间检查触摸是否在其边界之外并拒绝触摸。有关详细信息,请参阅 UIView hitTest:withEvent:

The problem is because you changed the scroll view's contentSize but did not change the frame of the contents view. Superviews participate in touch hit testing. You can place a subview outside of its superview bounds and by default the subview will not be clipped but touch events will not be delivered to it because the superview checks during hit testing that the touch is outside of its bounds and rejects the touch. See UIView hitTest:withEvent: for details.

这篇关于UIButton无法在UIScrollView的初始帧外部单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆