Skip to content

Commit 5895ece

Browse files
committed
Switched from modifying frame to using an offset
1 parent df105a7 commit 5895ece

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

Sources/KeyboardObserving/viewmodifier/KeyboardObserving.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ struct KeyboardObserving: ViewModifier {
1717

1818
func body(content: Content) -> some View {
1919
content
20-
.padding([.bottom], keyboardHeight)
20+
.offset(x: 0, y: -keyboardHeight)
2121
.edgesIgnoringSafeArea((keyboardHeight > 0) ? [.bottom] : [])
22-
.animation(.easeOut(duration: keyboardAnimationDuration))
2322
.onReceive(
2423
NotificationCenter.default.publisher(for: UIResponder.keyboardWillChangeFrameNotification)
2524
.receive(on: RunLoop.main),
@@ -30,17 +29,19 @@ struct KeyboardObserving: ViewModifier {
3029
func updateKeyboardHeight(_ notification: Notification) {
3130
guard let info = notification.userInfo else { return }
3231
// Get the duration of the keyboard animation
33-
keyboardAnimationDuration = (info[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double)
34-
?? 0.25
32+
keyboardAnimationDuration = 0.4
3533

3634
guard let keyboardFrame = info[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
3735
else { return }
38-
// If the top of the frame is at the bottom of the screen, set the height to 0.
39-
if keyboardFrame.origin.y == UIScreen.main.bounds.height {
40-
keyboardHeight = 0
41-
} else {
42-
// IMPORTANT: This height will _include_ the SafeAreaInset height.
43-
keyboardHeight = keyboardFrame.height + offset
36+
37+
withAnimation(.easeInOut(duration: keyboardAnimationDuration)) {
38+
// If the top of the frame is at the bottom of the screen, set the height to 0.
39+
if keyboardFrame.origin.y == UIScreen.main.bounds.height {
40+
keyboardHeight = 0
41+
} else {
42+
// IMPORTANT: This height will _include_ the SafeAreaInset height.
43+
keyboardHeight = keyboardFrame.height + offset
44+
}
4445
}
4546
}
4647
}

0 commit comments

Comments
 (0)