Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add padding on form segments in SwiftUI

In iOS 13, some native Apple apps use a list style I am struggling to recreate. It's basically a List within a Form containing Sections and some entries.

The only difference is that each Section has padding to the left and right side and a corner radius around the edges.

Here is an example from the Home app of what I would like to achieve (also used in the Timer tab in the Clock app):

iOS 13 Home app

Applying the .padding()-Modifier to the Form doesn't work.

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

I am wondering if it is at all possible in SwiftUI or if this is just some UIKit-adjustment on a UITableViewCell.

like image 704
amq Avatar asked Oct 17 '25 06:10

amq


2 Answers

This is new UITableView.Style called .insetGrouped. This is the documentation

You can set it with code:

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

Or with Interface builder:

Ki

SwiftUI doesn't have this style (yet), but in the future, it should be a ListStyle that you can use with .listStyle modifier on a list. Currently available styles are:

.listStyle(DefaultListStyle()) // wich is PlainListStyle
.listStyle(PlainListStyle())
.listStyle(GroupedListStyle())
// .listStyle(InsetGroupedListStyle()) // unresolved (yet)
like image 168
Mojtaba Hosseini Avatar answered Oct 19 '25 02:10

Mojtaba Hosseini


It is possible with this modification to Form or List:

Form {
  Text("Hey")
}
.listStyle(GroupedListStyle())
.environment(\.horizontalSizeClass, .regular)
like image 38
Nicolas Degen Avatar answered Oct 19 '25 03:10

Nicolas Degen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!