UIScrollView不使用UItextfields滚动 [英] UIScrollView not scrolling with UItextfields

查看:106
本文介绍了UIScrollView不使用UItextfields滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个普通视图,用户可以在其中更新其个人资料。我按照以下步骤制作了该视图

I am making a normal view where users can update their profiles. I followed these steps to make that view



  1. 创建了一个新的 UIViewController 使用xib文件

  2. 在超级视图中添加 UIScrollView

  3. 几乎已添加9 UITextField 和1个小 UIWebView

  4. 满足Autolayout约束,例如top UITextField 将具有顶部,左侧,右侧和高度约束,并且对于以下所有
    控件都相同,但最后 UITextField 具有顶部,左侧,右侧,底部和高度
    约束。

  1. Created a new UIViewController with xib file
  2. Added a UIScrollView in super view
  3. Added almost 9 UITextField and 1 small UIWebView
  4. Satisfied Autolayout constraints like top UITextField will have top, left, right and height constrains and same for all the following controls but last UITextField have top, left, right, bottom and height constraints.


现在所有约束应用和满足,但当我运行视图,然后尝试通过拖动 UITextField 滚动,然后scrollview不滚动,但如果我滚动从<$ c以外的某些区域滚动$ c> UITextField 然后滚动非常好。有人可以告诉我可能是什么主要问题吗?

Now all the constraints are applied and satisfied but when I run the view and then try to scroll by dragging UITextField then scrollview is not scrolling but if I scroll by dragging from some area other than UITextField then it is scrolling very nice. Can anybody tell me what can be the main problem?


注意:除了设置xib文件之外,没有其他代码。
此链接提供了一个示例项目 https:// www .dropbox.com / s / 7oqry8yzd9twnp1 / TestScroll.zip?dl = 0


推荐答案

重写UIScrollView touchesShouldCancelInContentView 方法将解决此问题。

Overriding UIScrollView touchesShouldCancelInContentView method will solve this problem.


根据Apple如果触摸已经传递到滚动视图的子视图
,则在
滚动开始之前调用touchesShouldCancelInContentView。如果它返回NO,则触摸将继续为
发送到子视图并且不会发生滚动。

According to Apple touchesShouldCancelInContentView is called before scrolling begins if touches have already been delivered to a subview of the scroll view. if it returns NO the touches will continue to be delivered to the subview and scrolling will not occur.

默认情况下这如果view是 UIControl ,则method返回NO。因此,UIControls不会发生滚动。

By default this method returns NO if view is a UIControl. So the scroll doesn't happens for the UIControls.

如果我们从此方法返回YES,则触摸将不会传递到子视图,因此将进行滚动。

If we returns YES from this method the touches will not be delivered to subview, So the scroll will occurs.

所以重写UIScrollView touchesShouldCancelInContentView 如下所示

So override UIScrollView touchesShouldCancelInContentView like following

@interface MyScrollView : UIScrollView

@end

@implementation MyScrollView

- (BOOL)touchesShouldCancelInContentView:(UIView *)view{
    return YES;
}

@end

注: touchesShouldCancelInContentView 方法仅在我们将 canCancelContentTouches 属性设置为YES时调用

NOTE: touchesShouldCancelInContentView method only calls if we set the canCancelContentTouches property to YES

希望这有帮助。

这篇关于UIScrollView不使用UItextfields滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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