UIScrollView not scrolling with UItextfields

[亡魂溺海] 提交于 2019-12-02 13:45:00

问题


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


回答1:


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.




回答2:


You need to add an extra parent View for all the textfield in ScrollView.

Note:

  • Give constraints to UIView w.r.t. UIScrollView as Leading, Trailing, Top, Bottom and Equal Widths.

  • Also don't forget to add the bottom constraint for last object in UIView w.r.t. UIView (Customize it to approx. 30 since it will not scroll much after finishing the contents).

  • Customize the constraint of UIView bottom w.r.t UIScrollView to 0.

See:




回答3:


Set delayContentTouchesProperty to YES for UIScrollView. This would let you scroll if you touch contents of UIScrollView. You can do it either programmatically or from storyboard.




回答4:


You should use uiview as a superview on uiscrollView ,put all the textfield on uiview and make sure you have all the satisfactory constraints applied on uiview as respect of uiscrollView.



来源:https://stackoverflow.com/questions/37156293/uiscrollview-not-scrolling-with-uitextfields

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