How to show complete List when keyboard is showing up in SwiftUI

前端 未结 7 569
广开言路
广开言路 2020-12-04 22:30

How is it possible to show the complete List when the keyboard is showing up? The keyboard is hiding the lower part of the list.

I have a textField in my list row. W

相关标签:
7条回答
  • 2020-12-04 23:19

    Updating the excellent Combine approach by Bogdan Farca to XCode 11.2:

    import Combine
    import SwiftUI
    
    struct AdaptsToSoftwareKeyboard: ViewModifier {
    
        @State var currentHeight: CGFloat = 0
    
        func body(content: Content) -> some View {
            content
                .padding(.bottom, self.currentHeight)
                .edgesIgnoringSafeArea(self.currentHeight == 0 ? Edge.Set() : .bottom)
                .onAppear(perform: subscribeToKeyboardEvents)
        }
    
        private let keyboardWillOpen = NotificationCenter.default
            .publisher(for: UIResponder.keyboardWillShowNotification)
            .map { $0.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect }
            .map { $0.height }
    
        private let keyboardWillHide =  NotificationCenter.default
            .publisher(for: UIResponder.keyboardWillHideNotification)
            .map { _ in CGFloat.zero }
    
        private func subscribeToKeyboardEvents() {
            _ = Publishers.Merge(keyboardWillOpen, keyboardWillHide)
                .subscribe(on: RunLoop.main)
                .assign(to: \.self.currentHeight, on: self)
        }
    }
    
    0 讨论(0)
提交回复
热议问题