UIScrollView: Scrolling content after ViewController View frame change

In a nutshell… I can scroll content however I’m unable to scroll to top to see “all” of the scrollView content after changing the ViewController view frame (the frame Origin Y value is decreased so the VC view moves upward to accommodate the keyboard).

On the storyboard ViewController I added a UIScrollview and a button below it. The scroll view is constrained to its parent (the VC) view. I add a contentView within the scrollview and constrain it to the scrollview and add equal width and height constraints between the content view and the scrollviews parent view. I then add textfields to the contentView and constrain them to the contentView. I calculate and set the scrollView content size so Im able to scroll the content only and not the entire contentView. So far so good.

I register for keyboard notifications and in the keyboard will show handler I decrease the view controller’s view origin Y value (via setFrame) which moves the view upward so the bottom of the view ends up just above the keyboard. Also in the keyboardWillShow handler I increase the scrollview contentsize by the vertical height of the keyboard since the scrollView area has decreased by the vertical height of the keyboard.

Im able to scroll the content however contentoffset (0,0) resides mid way in the content and the additional contentsize appears below the textfields content. Im no longer able to scroll up to the top of the content.

I know that the scrollView scrolls its content by changing its bounds so I tried to change the scrollview frame (decrease its origin Y value) and also tried the same for the contentview however when one of the textfields in the contentView becomesFirstResponder OR I just try to scroll with the mouse, the content scrolls down and the top half of the content is not visible and cannot be scrolled to (as mentioned contentoffset is (0,0) half way down the content and is “the top of the content”.

Does anyone see any glaring issues with my approach for continuing to scroll all of the content with the keyboard displayed and the ViewController origin Y value decreased?

hi @bahiabound,
have you considered using a pod that manages the keyboard for you? There are some good 3rd party libraries that allow you to work with forms and keyboard. I cannot recollect them from the top of my head.

cheers,

I needed to add contentInsets and scrollIndicatorInsets. Great article here… Using UIScrollView with Auto Layout in iOS

@bahiabound, glad that you got that to work

This topic was automatically closed after 166 days. New replies are no longer allowed.