ios - UIScrollView 不使用 UItextfields 滚动

标签 ios objective-c uiscrollview uitextfield

我正在制作一个普通 View ,用户可以在其中更新他们的个人资料。我按照这些步骤制作了该 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.


现在所有约束都已应用并得到满足,但是当我运行 View 然后尝试通过拖动 UITextField 进行滚动时然后 ScrollView 不滚动,但如果我通过从 UITextField 以外的某个区域拖动来滚动然后它滚动得很好。谁能告诉我主要问题是什么?

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

最佳答案

覆盖 UIScrollView touchesShouldCancelInContentView方法将解决这个问题。

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,则此方法返回 NO。 .所以 UIControls 不会发生滚动。

如果我们从这个方法返回 YES,触摸将不会被传递到 subview ,因此会发生滚动。

所以覆盖 UIScrollView touchesShouldCancelInContentView喜欢以下
@interface MyScrollView : UIScrollView

@end

@implementation MyScrollView

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

@end

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

希望这可以帮助。

关于ios - UIScrollView 不使用 UItextfields 滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37156293/

相关文章:

ios - 如何在 iOS 应用程序中连接 FMDB 中两个不同数据库的两个表

ios - iOS 中的棕褐色调 PDF

ios - If 语句不会检查 NSArray

objective-c - 如何在 mac book air 上的 iOS 模拟器中滚动?

ios - 使用自动布局时如何在 UIScrollView 中水平居中 UIView?

iphone - 通过 Facebook 登录后应用程序介绍如何下次跳过介绍?

ios - 具有部分匹配值的 Firebase iOS 查询数据

android - Ionic ios 应用程序在标题下方显示黑条

objective-c - 在 NSFetchRequest 中按实体名称排序

iphone - if else 语句混淆 Objective-C