Skip to content

Commit

Permalink
Constrain bottom anchor to keyboard frame height
Browse files Browse the repository at this point in the history
  • Loading branch information
mhoran committed Dec 31, 2024
1 parent aeef2cd commit 2a7e9af
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions modules/keyboard-avoiding-view/ios/KeyboardAvoidingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class KeyboardAvoidingView: ExpoView, UIGestureRecognizerDelegate {
return panGestureRecognizer
}()
private var isScrollViewPanning = false
private lazy var containerBottomAnchorConstraint: NSLayoutConstraint = {
return container.bottomAnchor.constraint(equalTo: bottomAnchor)
}()

required init(appContext: AppContext? = nil) {
super.init(appContext: appContext)
Expand All @@ -36,7 +39,7 @@ 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),
containerBottomAnchorConstraint,
])

NotificationCenter.default.addObserver(
Expand Down Expand Up @@ -72,7 +75,6 @@ class KeyboardAvoidingView: ExpoView, UIGestureRecognizerDelegate {
}

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

Expand Down Expand Up @@ -100,6 +102,8 @@ class KeyboardAvoidingView: ExpoView, UIGestureRecognizerDelegate {

UIView.animate(withDuration: animationDuration, delay: 0.0, options: animationOptions) {
self.scrollView?.setInsetsFromKeyboardHeight(keyboardHeight)
self.containerBottomAnchorConstraint.constant = -keyboardHeight
self.layoutIfNeeded()
}
}

Expand All @@ -108,7 +112,6 @@ class KeyboardAvoidingView: ExpoView, UIGestureRecognizerDelegate {
measurer.removeObserver(self, forKeyPath: "center")
measurerHasObserver = false
}
keyboardLayoutGuide.followsUndockedKeyboard = false
updateInsets(notification, closing: true)
}

Expand All @@ -123,6 +126,8 @@ class KeyboardAvoidingView: ExpoView, UIGestureRecognizerDelegate {
return
}
self.scrollView?.setInsetsFromKeyboardHeight(measurer.frame.height)
self.containerBottomAnchorConstraint.constant = -keyboardHeight
self.layoutIfNeeded()
}
}

Expand Down

0 comments on commit 2a7e9af

Please sign in to comment.