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

前端 未结 7 574
广开言路
广开言路 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:10

    there is an answer here to handle keyboard actions, you can subscribe for keyboard events like this:

    final class KeyboardResponder: BindableObject {
        let didChange = PassthroughSubject()
        private var _center: NotificationCenter
        private(set) var currentHeight: CGFloat = 0 {
            didSet {
                didChange.send(currentHeight)
            }
        }
    
        init(center: NotificationCenter = .default) {
            _center = center
            _center.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
            _center.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
        }
    
        deinit {
            _center.removeObserver(self)
        }
    
        @objc func keyBoardWillShow(notification: Notification) {
            if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
                currentHeight = keyboardSize.height
            }
        }
    
        @objc func keyBoardWillHide(notification: Notification) {
            currentHeight = 0
        }
    }
    

    and then just use it like this:

    @State var keyboard = KeyboardResponder()
    var body: some View {
            List {
                VStack {
                 ...
                 ...
                 ...
                }.padding(.bottom, keyboard.currentHeight)
    }
    

提交回复
热议问题