Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SwiftUI
- import Combine
- struct AdaptsToSoftwareKeyboard: ViewModifier {
- @State var currentHeight: CGFloat = 0
- func body(content: Content) -> some View {
- content
- .padding(.bottom, currentHeight)
- .edgesIgnoringSafeArea(.bottom)
- .onAppear(perform: subscribeToKeyboardEvents)
- }
- private func subscribeToKeyboardEvents() {
- NotificationCenter.Publisher(
- center: NotificationCenter.default,
- name: UIResponder.keyboardWillShowNotification
- ).compactMap { notification in
- notification.userInfo?["UIKeyboardFrameEndUserInfoKey"] as? CGRect
- }.map { rect in
- rect.height
- }.subscribe(Subscribers.Assign(object: self, keyPath: \.currentHeight))
- NotificationCenter.Publisher(
- center: NotificationCenter.default,
- name: UIResponder.keyboardWillHideNotification
- ).compactMap { notification in
- CGFloat.zero
- }.subscribe(Subscribers.Assign(object: self, keyPath: \.currentHeight))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement