UIScrollView不使用UItextfields滚动

UIScrollView不使用UItextfields滚动

本文介绍了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?


推荐答案

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

Overriding UIScrollView touchesShouldCancelInContentView method will solve this problem.

默认情况下这如果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滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:57