Search code examples
iosswiftuiscrollviewuipageviewcontroller

Is it possible to use a pageviewcontroller inside a scrollview?


I have a pageviewcontroller, which scrolls horizontally, displaying questions and answers.

The contentView for the pageviewcontroller is populated with a scrollview, and inside of this is some stack views, one of which has a textView, for user entry.

enter image description here

When the user taps on the textView, the keyboard pops up, sometimes covering up the textView. When I try to scroll vertically, as I would expect scrollview to allow, the scrollview doesn't respond, I can only scroll horizontally to the next page view.

The problem is that when the keyboard pops up after the user taps in the textview, the textview may be hidden underneath the keyboard, so the user can't see what he's typing. I'd like to have the view scroll upward when he keyboard is tapped, so the user can see what is being typed.


Solution

  • That's called "Keyboard managing" You have to:

    1) Set observer for keyboard appear and keyboard disappear action

    2) Update bottom constraint, when keyboard appears.

    3) Update it again, when keyboard disappears.

    4) Delete observers, when view will disappear.

    Step by step instructions, for example here: Move view with keyboard using Swift

    But my recommendation is to update constraints, not origins, as in instruction. Or, maybe, you can set an offset for content