Scroll View School · Keyboard Insets |

This is a companion discussion topic for the original entry at

What I’m noticing is that on an iPhone X, iPhone XS simulators etc , if you focus on the field to type the e-mail, the keyboard pops up and the view shifts upwards as expected to see the textfield, however, if you start typing characters, the view starts to scroll upwards automatically and you won’t be able to see the textfield anymore.

Have you noticed this behavior?

I believe I did see that when recording this course, but I couldn’t reliably reproduce it, and haven’t seen it at all on-device. I had a lot of trouble in general with this batch of X simulators and scroll views, but none of the issues carried over to builds on my own iPhone X :woman_shrugging:t4:.

@catie What if the textfield is not hidden behind the keyboard?
What would I have to put a s a conditional? For example I have a nested stack view in a scrollview. would I have to convert points first? etc?

@catie @jessycatterwaul @jcatterwaul Can you please help with this when you get a chance? Thank you - much appreciated! :]

@shogunkaramazov I’ve come up with a solution that works for nested textfields inside stack views, inside UIViews, inside scrollviews etc.

    @objc func keyboardShown(notification: Notification) {
        if let keyBoardHeight = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size.height, let textField = activeTextField {
            // Cut view height so we can check if the textfields origin is visible
            view.frame.size.height -= keyBoardHeight
            // Use maxY to get the bottom of textfield so it's entirely visible.
            let point = textField.convert(CGPoint(x: textField.frame.minX, y: textField.frame.maxY), to: view)
            if !view.point(inside: point, with: nil) {
                scrollView.contentInset.bottom += keyBoardHeight + 30 // extra padding taking into account textfield height.
            // set frame size back after it updates.
            view.frame.size.height += keyBoardHeight
    @objc func keyboardDismissed(notification: Notification) {
        scrollView.contentInset = .zero

I was just coming in to reply to your question, but it looks like you worked it out :]

Thanks for sharing your solution!

@catie Thanks! This also takes into account if the textfield is already visible and still visible if the keyboard shows up.
it took hours of trial and error of scrollViewToVisibleRect and other things but this works perfectly :slight_smile:

1 Like

Hi, is this the behavior that you are noticing? This is how the “solution” Keyboard - End project runs in the iPhone X simulator on my Xcode 10.2.1 under Mojave. I’ve found this runs the same way on simulators tested (X, Xs, SE), but I assume it’s consistent with all.
Curiously, when I built & ran on my own iPhone X, everything worked as expected.

Yes, that was the behavior. It also happened if you start typing into the text field. The problem did not happen to me on actual iPhone devices.