Sor*_*ica 12 ios swift swiftui
TextField("Name", text: $name)
Run Code Online (Sandbox Code Playgroud)
我找到了这个方法
func focusable(_ isFocusable: Bool = true, onFocusChange: @escaping (Bool) -> Void = { _ in }) -> some View
Run Code Online (Sandbox Code Playgroud)
但它仅适用于 MacOS,我怎么能为 iOS 做类似的事情?
paw*_*222 56
在 iOS 15 中,我们可以通过以下方式检测更改@FocusState:
@FocusState private var isFocused: Bool
...
var body: some View {
Form {
TextField("Name", text: $name)
.focused($isFocused)
.onChange(of: isFocused) { isFocused in
// ...
}
}
}
Run Code Online (Sandbox Code Playgroud)
Sub*_*pan 18
您可以使用init(_:text:onEditingChanged:onCommit:)文本字段中的初始值设定项。在那里您将在开始编辑和结束编辑时触发操作。您可以在下面找到最小的示例。
import SwiftUI
struct ContentView: View {
@State private var greeting: String = "Hello world!"
var body: some View {
TextField("Welcome", text: $greeting, onEditingChanged: { (editingChanged) in
if editingChanged {
print("TextField focused")
} else {
print("TextField focus removed")
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
8648 次 |
| 最近记录: |