UIScrollView 不使用 UItextfields 滚动

UIScrollView not scrolling with UItextfields(UIScrollView 不使用 UItextfields 滚动)

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

问题描述

我正在制作一个普通视图,用户可以在其中更新他们的个人资料.我按照这些步骤制作了该视图

<块引用>

  1. 使用 xib 文件创建了一个新的 UIViewController
  2. 在超级视图中添加了一个 UIScrollView
  3. 添加了近 9 个 UITextField 和 1 个小的 UIWebView
  4. 满足自动布局约束,如顶部 UITextField 将具有顶部、左侧、右侧和高度约束,以下所有约束都相同控件,但最后一个 UITextField 具有顶部、左侧、右侧、底部和高度约束.

现在所有约束都已应用并得到满足,但是当我运行视图然后尝试通过拖动 UITextField 滚动时,scrollview 不会滚动,但如果我通过从 UITextField 然后它滚动得很好.谁能告诉我主要问题是什么?

<块引用>

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

解决方案

重写 UIScrollView touchesShouldCancelInContentView 方法可以解决这个问题.

<块引用>

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

默认情况下,如果视图是 UIControl,则此方法返回 NO.所以 UIControls 不会发生滚动.

如果我们从这个方法返回YES,触摸将不会被传递到子视图,所以会发生滚动.

所以像下面这样覆盖 UIScrollView touchesShouldCancelInContentView

@interface MyScrollView : UIScrollView@结尾@implementation MyScrollView- (BOOL)touchesShouldCancelInContentView:(UIView *)view{返回是;}@结尾

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

希望这会有所帮助.

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

  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.

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?

Note: there is no code yet other than setting up xib file. A sample project is available on this link https://www.dropbox.com/s/7oqry8yzd9twnp1/TestScroll.zip?dl=0

解决方案

Overriding UIScrollView touchesShouldCancelInContentView method will solve this problem.

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.

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

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

So override UIScrollView touchesShouldCancelInContentView like following

@interface MyScrollView : UIScrollView

@end

@implementation MyScrollView

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

@end

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

Hope this helps.

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

本文标题为:UIScrollView 不使用 UItextfields 滚动

基础教程推荐