Skip to content

Commit 2647c5d

Browse files
committed
Switched from modifying frame to using an offset
1 parent df105a7 commit 2647c5d

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

Sources/KeyboardObserving/viewmodifier/KeyboardObserving.swift

Lines changed: 10 additions & 8 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),
@@ -35,12 +34,15 @@ struct KeyboardObserving: ViewModifier {
3534

3635
guard let keyboardFrame = info[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
3736
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
37+
38+
withAnimation(.easeInOut(duration: keyboardAnimationDuration)) {
39+
// If the top of the frame is at the bottom of the screen, set the height to 0.
40+
if keyboardFrame.origin.y == UIScreen.main.bounds.height {
41+
keyboardHeight = 0
42+
} else {
43+
// IMPORTANT: This height will _include_ the SafeAreaInset height.
44+
keyboardHeight = keyboardFrame.height + offset
45+
}
4446
}
4547
}
4648
}

0 commit comments

Comments
 (0)