如何在 SwiftUI 中的表单段上添加填充

Posted

技术标签:

【中文标题】如何在 SwiftUI 中的表单段上添加填充【英文标题】:How to add padding on form segments in SwiftUI 【发布时间】:2019-10-09 19:14:49 【问题描述】:

ios 13 中,一些原生 Apple 应用程序使用我正在努力重新创建的列表样式。它基本上是 Form 中的 List,其中包含 Sections 和一些条目。

唯一的区别是每个Section 的左侧和右侧都有填充,边缘有一个角半径。

这是我想要实现的 Home 应用程序的一个示例(也用于时钟应用程序的计时器选项卡中):

.padding()-Modifier 应用于Form 不起作用。

struct ContentView: View 
   var body: some View 
      Form 
         Section 
            Text("foo")
            Text("bar")
         
         Section 
            Text("foo")
         
         Section 
            Text("bar")
         
      
   

我想知道在 SwiftUI 中是否有可能,或者这只是对 UITableViewCell 的一些 UIKit 调整。

【问题讨论】:

【参考方案1】:

这是新的UITableView.Style,称为.insetGrouped。这是the documentation

你可以用代码设置:

let tableView = UITableView(frame: frame, style: .insetGrouped)

或使用界面生成器:

SwiftUI 还没有这种风格,但在未来,它应该是一个ListStyle,您可以在列表中与.listStyle 修饰符一起使用。目前可用的样式有:

.listStyle(DefaultListStyle()) // wich is PlainListStyle
.listStyle(PlainListStyle())
.listStyle(GroupedListStyle())
// .listStyle(InsetGroupedListStyle()) // unresolved (yet)

【讨论】:

【参考方案2】:

可以对FormList 进行此修改:

Form 
  Text("Hey")

.listStyle(GroupedListStyle())
.environment(\.horizontalSizeClass, .regular)

【讨论】:

以上是关于如何在 SwiftUI 中的表单段上添加填充的主要内容,如果未能解决你的问题,请参考以下文章