Skip to content

Commit

Permalink
Always follow undocked keyboard, activate constraints at bottom
Browse files Browse the repository at this point in the history
  • Loading branch information
mhoran committed Jan 1, 2025
1 parent 5298e39 commit 61d16e3
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions modules/keyboard-avoiding-view/ios/KeyboardAvoidingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class KeyboardAvoidingView: ExpoView, UIGestureRecognizerDelegate {
container.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
measurer.topAnchor.constraint(equalTo: keyboardLayoutGuide.topAnchor),
measurer.leadingAnchor.constraint(equalTo: leadingAnchor),
measurer.trailingAnchor.constraint(equalTo: trailingAnchor),
measurer.bottomAnchor.constraint(equalTo: bottomAnchor),
Expand All @@ -37,9 +36,26 @@ class KeyboardAvoidingView: ExpoView, UIGestureRecognizerDelegate {
container.heightAnchor.constraint(equalTo: heightAnchor),
container.leadingAnchor.constraint(equalTo: leadingAnchor),
container.trailingAnchor.constraint(equalTo: trailingAnchor),
container.bottomAnchor.constraint(equalTo: keyboardLayoutGuide.topAnchor),
])

keyboardLayoutGuide.followsUndockedKeyboard = true

let measurerTopToKeyboard = keyboardLayoutGuide.topAnchor.constraint(equalTo: measurer.topAnchor)
measurerTopToKeyboard.identifier = "measurerTopToKeyboard"

let containerBottomToKeyboard = keyboardLayoutGuide.topAnchor.constraint(equalTo: container.bottomAnchor)
containerBottomToKeyboard.identifier = "containerBottomToKeyboard"

keyboardLayoutGuide.setConstraints([measurerTopToKeyboard, containerBottomToKeyboard], activeWhenNearEdge: .bottom)

let measurerTopToViewBottom = bottomAnchor.constraint(equalTo: measurer.topAnchor)
measurerTopToViewBottom.identifier = "measurerTopToViewBottom"

let containerBottomToViewBottom = bottomAnchor.constraint(equalTo: container.bottomAnchor)
containerBottomToViewBottom.identifier = "containerBottomToViewBottom"

keyboardLayoutGuide.setConstraints([measurerTopToViewBottom, containerBottomToViewBottom], activeWhenAwayFrom: .bottom)

NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillShow),
Expand Down Expand Up @@ -80,7 +96,6 @@ class KeyboardAvoidingView: ExpoView, UIGestureRecognizerDelegate {
}

@objc private func keyboardWillShow(_ notification: Notification) {
keyboardLayoutGuide.followsUndockedKeyboard = true
updateInsets(notification)
}

Expand All @@ -93,7 +108,11 @@ class KeyboardAvoidingView: ExpoView, UIGestureRecognizerDelegate {
}

@objc private func keyboardWillChangeFrame(_ notification: Notification) {
if (!isKeyboardShown) {
let userInfo = notification.userInfo

guard let keyboardIsLocal = userInfo?[UIResponder.keyboardIsLocalUserInfoKey] as? Bool else { return }

if (!isKeyboardShown || !keyboardIsLocal) {
return
}
updateInsets(notification)
Expand Down Expand Up @@ -127,7 +146,6 @@ class KeyboardAvoidingView: ExpoView, UIGestureRecognizerDelegate {
measurer.removeObserver(self, forKeyPath: "center")
measurerHasObserver = false
}
keyboardLayoutGuide.followsUndockedKeyboard = false
isKeyboardShown = false
updateInsets(notification, closing: true)
}
Expand Down

0 comments on commit 61d16e3

Please sign in to comment.