【问题标题】:SwiftUI form section title and rows have incorrect formattingSwiftUI 表单部分标题和行的格式不正确
【发布时间】:2021-08-04 12:51:03
【问题描述】:

以下 SwiftUI 表单将错误地呈现该部分的标题和内容。部分标题应大写并具有背景颜色。行应该像列表一样标记,这意味着它们应该有列表分隔符和填充。

struct ContentView: View {
    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("Test")) {
                    ForEach(0..<5) { index in
                        Text("Row \(index)")
                    }
                }
                .navigationBarItems(trailing: Button(action: {}, label: { Text("Dummy") }))
            }
            .navigationTitle(Text("Navigation title"))
        }
    }
}

结果:

【问题讨论】:

    标签: swiftui swiftui-form


    【解决方案1】:

    格式不正确的原因是.navigationBarItems不应该附加到Section,而是向下移动到Form

    Form {
        Section(header: Text("Test")) {
            ForEach(0..<5) { index in
                Text("Row \(index)")
            }
        }
    }
    .navigationBarItems(trailing: Button(action: {}, label: { Text("Dummy") }))
    .navigationTitle(Text("Navigation title"))
    

    这会正确呈现表单:

    如果您使用较新的.toolbar 修饰符并将其附加到Section,该部分将正确呈现,但按钮根本不会显示。但是.toolbar 只能在 iOS 14 及更高版本中使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2019-01-29
      • 2021-12-20
      相关资源
      最近更新 更多