[英]How to detect if keyboard is present in swiftui
I want to know if the keyboard is present when the button is pressed.我想知道按下按钮时键盘是否存在。 How would I do this?
我该怎么做? I have tried but I don't have any luck.
我已经尝试过,但我没有任何运气。 Thanks.
谢谢。
Using this protocol, KeyboardReadable
, you can conform to any View
and get keyboard updates from it.使用此协议
KeyboardReadable
,您可以符合任何View
并从中获取键盘更新。
KeyboardReadable
protocol: KeyboardReadable
协议:
import Combine
import UIKit
/// Publisher to read keyboard changes.
protocol KeyboardReadable {
var keyboardPublisher: AnyPublisher<Bool, Never> { get }
}
extension KeyboardReadable {
var keyboardPublisher: AnyPublisher<Bool, Never> {
Publishers.Merge(
NotificationCenter.default
.publisher(for: UIResponder.keyboardWillShowNotification)
.map { _ in true },
NotificationCenter.default
.publisher(for: UIResponder.keyboardWillHideNotification)
.map { _ in false }
)
.eraseToAnyPublisher()
}
}
It works by using Combine and creating a publisher so we can receive the keyboard notifications.它通过使用组合并创建发布者来工作,以便我们可以接收键盘通知。
With an example view of how it can be applied:以如何应用它的示例视图为例:
struct ContentView: View, KeyboardReadable {
@State private var text: String = ""
@State private var isKeyboardVisible = false
var body: some View {
TextField("Text", text: $text)
.onReceive(keyboardPublisher) { newIsKeyboardVisible in
print("Is keyboard visible? ", newIsKeyboardVisible)
isKeyboardVisible = newIsKeyboardVisible
}
}
}
You can now read from the isKeyboardVisible
variable to know if the keyboard is visible.您现在可以从
isKeyboardVisible
变量中读取以了解键盘是否可见。
When the TextField
is active with the keyboard showing, the following prints:当
TextField
处于活动状态并显示键盘时,将打印以下内容:
Is keyboard visible?
键盘可见吗? true
真的
When the keyboard is then hidden upon hitting return, the following prints instead:当键盘在按回车时隐藏时,将打印以下内容:
Is keyboard visible?
键盘可见吗? false
错误的
You can use keyboardWillShowNotification
/ keyboardWillHideNotification
to update as soon as they keyboard starts to appear or disappear, and the keyboardDidShowNotification
/ keyboardDidHideNotification
variants to update after the keyboard has appeared or disappeared.您可以使用
keyboardWillShowNotification
/ keyboardWillHideNotification
在键盘开始出现或消失时立即更新,而keyboardDidShowNotification
/ keyboardDidHideNotification
变体在键盘出现或消失后进行更新。 I prefer the will
variant because the updates are instant for when the keyboard shows.我更喜欢
will
变体,因为当键盘显示时更新是即时的。
You can use the focused(_:)
view modifier and @FocusState
property wrapper to know whether a text field is editing, and also change the editing state.您可以使用
focused(_:)
视图修饰符和@FocusState
属性包装器来了解文本字段是否正在编辑,还可以更改编辑state。
@State private var text: String = ""
@FocusState private var isTextFieldFocused: Bool
var body: some View {
VStack {
TextField("hello", text: $text)
.focused($isTextFieldFocused)
if isTextFieldFocused {
Button("Keyboard is up!") {
isTextFieldFocused = false
}
}
}
}
My little improvement @George's answer.我的小改进@George 的回答。
Implement publisher right inside the View protocol在 View 协议中实现发布者
extension View {
var keyboardPublisher: AnyPublisher<Bool, Never> {
Publishers
.Merge(
NotificationCenter
.default
.publisher(for: UIResponder.keyboardWillShowNotification)
.map { _ in true },
NotificationCenter
.default
.publisher(for: UIResponder.keyboardWillHideNotification)
.map { _ in false })
.debounce(for: .seconds(0.1), scheduler: RunLoop.main)
.eraseToAnyPublisher()
}
}
I also added debounce operator in order to prevent true - false toggle when you have multiple TextFields and user moves between them.我还添加了 debounce 运算符,以防止当您有多个 TextField 并且用户在它们之间移动时进行 true - false 切换。
Use in any View在任何视图中使用
struct SwiftUIView: View {
@State var isKeyboardPresented = false
@State var firstTextField = ""
@State var secondTextField = ""
var body: some View {
VStack {
TextField("First textField", text: $firstTextField)
TextField("Second textField", text: $secondTextField)
}
.onReceive(keyboardPublisher) { value in
isKeyboardPresented = value
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.