Show/hide 键盘时的 navigationBarItems visible/non-visible |斯威夫特用户界面

Show/hide navigationBarItems when keyboard visible/non-visible | SwiftUI

我花了很多时间研究和尝试不同的代码来完成这项工作,但似乎无法弄清楚。下面您将看到两段代码,一段是我创建 "Done" 栏按钮项,另一段是检查键盘是否存在并在单击按钮时关闭它。我遇到的一个问题是,我只想在键盘出现时显示条形按钮。我希望在键盘消失后或什至首先打开键盘之前隐藏条形按钮。你现在如何使用 SwiftUI 做到这一点?

.navigationBarItems(trailing:
            Button(action: {
                if UIApplication.shared.isKeyboardPresented {
                    UIApplication.shared.endEditing()
                }
            }, label: {
                Text("Done")
            })

extension UIApplication {
    func endEditing() {
        sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
    /// Checks if view hierarchy of application contains `UIRemoteKeyboardWindow` if it does, keyboard is presented
    var isKeyboardPresented: Bool {
        if let keyboardWindowClass = NSClassFromString("UIRemoteKeyboardWindow"),
            self.windows.contains(where: { [=12=].isKind(of: keyboardWindowClass) }) {
            return true
        } else {
            return false
        }
    }
}

作为一个选项,您可以定义一个对象来监听键盘通知:

class KeybordManager: ObservableObject {
    static let shared = KeybordManager()

    @Published var keyboardFrame: CGRect? = nil

    init() {
        let notificationCenter = NotificationCenter.default
        notificationCenter.addObserver(self, selector: #selector(willHide), name: UIResponder.keyboardWillHideNotification, object: nil)
        notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
    }

    @objc func willHide() {
        self.keyboardFrame = .zero
    }

    @objc func adjustForKeyboard(notification: Notification) {
        guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }

        let keyboardScreenEndFrame = keyboardValue.cgRectValue
        self.keyboardFrame = keyboardScreenEndFrame
    }
}

然后在您的视图中订阅 keyboardFrame 更新并相应地显示和隐藏 Done 按钮:

public struct ContentView: View {

    @State private var showDoneButton = false
    @State private var text = ""

    public var body: some View {
        NavigationView {
            TextField("Some text", text: $text)
            .navigationBarItems(trailing:
                Group {
                    if self.showDoneButton {
                        Button(action: {
                            UIApplication.shared.endEditing()
                        }, label: {
                            Text("Done")
                        })
                    } else {
                        EmptyView()
                    }
            }

            )
                .onReceive(KeybordManager.shared.$keyboardFrame) { keyboardFrame in
                    if let keyboardFrame = keyboardFrame, keyboardFrame != .zero {
                        self.showDoneButton = true
                    } else {
                        self.showDoneButton = false
                    }
            }
        }
    }
}

extension UIApplication {
    func endEditing() {
        sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}