Pet*_*rbo 6 keyboard textfield swiftui ios16
我在 Xcode 14 beta 中遇到一个问题,正如您在下面的图像中看到的那样,输入一些文本后键盘会消失,而在 iOS 15 中键盘保持在原位,这是我想要的行为。
我正在做的是.onSubmit创建一个新项目并以编程方式设置它的焦点。
iOS 15(Xcode 13.4.1)
iOS 16(Xcode 14 测试版 3)
再次:
enum Focusable: Hashable {
case none
case row(id: UUID)
}
extension View {
func sync<T: Equatable>(_ field1: Binding<T>, _ field2: FocusState<T>.Binding ) -> some View {
self
.onChange(of: field1.wrappedValue) {
field2.wrappedValue = $0
}
.onChange(of: field2.wrappedValue) {
field1.wrappedValue = $0
}
}
}
class Store: ObservableObject {
struct Item: Identifiable {
var id = UUID()
var name: String
}
@Published var items = [Item]()
@Published var focusedItem: Focusable?
func createNewItem() {
let newItem = Item(name: "")
items.append(newItem)
focusedItem = .row(id: newItem.id)
}
}
struct ContentView: View {
@FocusState private var focusedItem: Focusable?
@StateObject var store = Store()
var body: some View {
NavigationView {
List {
ForEach($store.items) { $item in
TextField("", text: $item.name)
.focused($focusedItem, equals: .row(id: item.id))
.onSubmit(store.createNewItem)
}
}
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("New item") {
store.createNewItem()
}
}
}
.sync($store.focusedItem, $focusedItem)
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1043 次 |
| 最近记录: |