UIScrollView not scrolling with UItextfields(UIScrollView 不使用 UItextfields 滚动)
问题描述
我正在制作一个普通视图,用户可以在其中更新他们的个人资料.我按照这些步骤制作了该视图
<块引用>- 使用 xib 文件创建了一个新的
UIViewController
- 在超级视图中添加了一个
UIScrollView
- 添加了近 9 个
UITextField
和 1 个小的UIWebView
- 满足自动布局约束,如顶部
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
- Created a new
UIViewController
with xib file- Added a
UIScrollView
in super view- Added almost 9
UITextField
and 1 smallUIWebView
- Satisfied Autolayout constraints like top
UITextField
will have top, left, right and height constrains and same for all the following controls but lastUITextField
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 滚动
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01