SwiftUI-当放置在macOS上的列表中时,TextField被禁用(不可编辑) [英] SwiftUI - TextField is disabled (not editable) when placed in List on macOS

查看:101
本文介绍了SwiftUI-当放置在macOS上的列表中时,TextField被禁用(不可编辑)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当放置在macOS上的列表中时,TextField被禁用(不可编辑).当为iOS构建相同的代码并在Simulator中运行时,它可以按预期工作.

TextField is disabled (not editable) when placed in List on macOS. When the same code is build for iOS and ran in Simulator, it works as expected.

这是一个错误,还是我错过了什么?

Is this a bug, or am I missing something?

代码:

struct ContentView : View {

    @State private var text: String = ""

    var body: some View {
        VStack {
            List {
                // TextField is not editable when this code is ran on macOS
                TextField($text, placeholder: Text("Entry text"))
                Text("Entered text: \(text)")
            }
            // TextField is editable on both macOS as well as iOS
            TextField($text, placeholder: Text("Entry text"))
        }
    }
}

推荐答案

这是因为列表利用点击来驱动选择,而您在这里没有使用它.TextField仅在麦斯卡斯的列表中可编辑,只有当储存它的行有选择时才.

That's because the list is taking clicks to drive selection, which you're not using here. TextField becomes editable in the list on macOS only when the row in which it is housed has selection.

如果您将代码更改为类似的内容

If you change your code to something like this

struct ContentView : View {
    @State private var text: String = "Hello"
    @State private var selection: Int? = nil
    var body: some View {
        VStack {
            List(selection: $selection) {
                ForEach(0..<5) { _ in
                    TextField(self.$text)
                }
            }
            TextField($text)
        }
    }
}

然后运行代码,第一次单击单元格将使其被选中,第二次单击将使文本字段获得焦点.

then run the code, the first click on the cell will cause it to get selected and the second click will cause the text field to receive focus.

这篇关于SwiftUI-当放置在macOS上的列表中时,TextField被禁用(不可编辑)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆