我花了很多时间研究和使用不同的代码来完成这项工作,但似乎无法弄清楚。下面你会看到两段代码,一段是我创建“完成”栏按钮项,另一段是检查键盘是否存在,并在单击按钮时关闭它。我遇到的一个问题是,我只希望栏按钮在键盘出现时显示。我希望在键盘消失后或者甚至在首先打开键盘之前隐藏条形按钮。你现在如何使用 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: { $0.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
}
}
然后在您的 View 中订阅 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)
}
}
关于xcode - 当键盘可见/不可见时显示/隐藏 navigationBarItems | SwiftUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60383632/