nca
nca

Reputation: 445

ScrollView is not working properly with showing/hiding keyboard

Step 1: I have a textfield which is embedded in a scrollview, when I start editing the textfield, keyboard appears and I am changing scrollview insets accordingly.

Step 2: while keyboard is active I presented a viewcontroller, and came back.

step 3: Now if I start editing textfield again, the scrollview is stuck and not moving up as it was earlier.

Upvotes: 1

Views: 441

Answers (1)

Lawliet
Lawliet

Reputation: 3499

After you add observers with two selectors keyboardWillShow and keyboardWillShow to the NotificationCenter.default you can try this

func keyboardWillShow(_ notification: NSNotification) {
    super.keyboardWillShow(notification)

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        scrollView.contentInset.bottom = keyboardSize.height
    }
}

func keyboardWillHide(_ notification: NSNotification) {
    super.keyboardWillHide(notification)

    scrollView.contentInset.bottom = 0
}

Upvotes: 1

Related Questions