Skip to content

Commit

Permalink
Manually constrain bottomAnchor to match animation
Browse files Browse the repository at this point in the history
  • Loading branch information
mhoran committed Dec 30, 2024
1 parent be85eda commit e178939
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions modules/keyboard-avoiding-view/ios/KeyboardAvoidingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ class KeyboardAvoidingView: ExpoView {
private var scrollView: ScrollViewWrapper?
private var animationInProgress = false
private var measurerHasObserver = false
private var containerBottomAnchor: NSLayoutConstraint

required init(appContext: AppContext? = nil) {
containerBottomAnchor = container.bottomAnchor.constraint(equalTo: bottomAnchor)

super.init(appContext: appContext)

addSubview(measurer)
Expand All @@ -30,7 +33,7 @@ class KeyboardAvoidingView: ExpoView {
container.heightAnchor.constraint(equalTo: heightAnchor),
container.leadingAnchor.constraint(equalTo: leadingAnchor),
container.trailingAnchor.constraint(equalTo: trailingAnchor),
container.bottomAnchor.constraint(equalTo: keyboardLayoutGuide.topAnchor),
containerBottomAnchor,
])

NotificationCenter.default.addObserver(
Expand All @@ -53,17 +56,6 @@ class KeyboardAvoidingView: ExpoView {
name: UIResponder.keyboardWillHideNotification,
object: nil
)

NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardDidHide),
name: UIResponder.keyboardDidHideNotification,
object: nil
)

if #available(iOS 17.0, *) {
keyboardLayoutGuide.usesBottomSafeArea = false
}
}

deinit {
Expand All @@ -73,7 +65,6 @@ class KeyboardAvoidingView: ExpoView {
}

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

Expand Down Expand Up @@ -105,6 +96,8 @@ class KeyboardAvoidingView: ExpoView {
withDuration: animationDuration, delay: 0.0, options: animationOptions,
animations: {
self.scrollView?.setInsetsFromKeyboardHeight(keyboardHeight)
self.containerBottomAnchor.constant = -keyboardHeight
self.container.layoutIfNeeded()
},
completion: { finished in
self.animationInProgress = false
Expand All @@ -119,10 +112,6 @@ class KeyboardAvoidingView: ExpoView {
updateInsets(notification, closing: true)
}

@objc private func keyboardDidHide(_ notification: Notification) {
keyboardLayoutGuide.followsUndockedKeyboard = false
}

@objc override public func observeValue(
forKeyPath keyPath: String?,
of object: Any?,
Expand All @@ -133,7 +122,9 @@ class KeyboardAvoidingView: ExpoView {
if animationInProgress {
return
}
self.scrollView?.setInsetsFromKeyboardHeight(measurer.frame.height)
scrollView?.setInsetsFromKeyboardHeight(measurer.frame.height)
containerBottomAnchor.constant = measurer.frame.height
container.layoutIfNeeded()
}
}

Expand Down

0 comments on commit e178939

Please sign in to comment.