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

后端 未结 4 794
轻奢々
轻奢々 2021-01-06 11:40

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 mo

相关标签:
4条回答
  • 2021-01-06 11:57

    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)
      }
    }
    
    0 讨论(0)
  • 2021-01-06 12:01

    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.

    0 讨论(0)
  • 2021-01-06 12:05

    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)
        }
    
    }
    
    0 讨论(0)
  • 2021-01-06 12:13

    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
    }
    
    0 讨论(0)
提交回复
热议问题