In iOS - When touching a text field in a scroll view the view can not scroll

时间秒杀一切 提交于 2019-12-02 01:53:52

问题


When trying to scroll a view by touching and dragging on a text field, similarly to how you would scroll around on the edit contacts view on the iPhone, the view will not move, touching and dragging in between the text fields or off to the side of the text fields in the scroll view works but not when touching a textfield.

Any insights would be greatly appreciated. Thanks!


回答1:


Try setting the delaysContentTouches property to YES for the UIScrollView to which you have added to text field. This should allow you to scroll if you touch and then drag within a text field.




回答2:


Setting delaysContentTouches to true didn't work for me either. What did was iterating through the scrollview's gesture recognizers and setting each of their delaysTouchesBegan and cancelsTouchesInView to true and false, respectively:

scrollView.gestureRecognizers?.forEach {

    $0.delaysTouchesBegan = true; $0.cancelsTouchesInView = false
}



回答3:


I think it solution in this situation

extension UITableView {

    override public func touchesShouldCancelInContentView(view: UIView) -> Bool {
        if view is UITextField || view is UITextView {
            return true
        }
        return super.touchesShouldCancelInContentView(view)
    }

}



回答4:


try with below approach. Just replace your UIScrollView with ScrollView and adds the code to your project.

SWIFT 4.1

class ScrollView: UIScrollView {
  override func touchesShouldCancel(in view: UIView) -> Bool {
    if type(of: view) == TextField.self || type(of: view) == UITextView.self {
      return true
    }
    return super.touchesShouldCancel(in: view)
  }
}


来源:https://stackoverflow.com/questions/12126860/in-ios-when-touching-a-text-field-in-a-scroll-view-the-view-can-not-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!