How can one change the row height?

Viewed 291

With the new Table introduced in SwiftUI 3, how can one change the row height?

struct ContentView: View {
    @State private var items: [ItemModel] = Array(0...100).map { ItemModel(title: "item \($0)") }
    @State private var selection = Set<ItemModel.ID>()

    var body: some View {
        Table(selection: $selection) {
            TableColumn("title", value: \.title)
        } rows: {
            ForEach(items) { item in
                TableRow(item)
            }
        }
    }
}

struct ItemModel: Identifiable {
    let id = UUID()
    let title: String
}

For the TableColumn, only a width() modifier is available. But for the TableRow one cannot specify the height. Is there really no way to provide the height for a TableRow?

1 Answers

You can set the height with padding modifiers in the content of all TableColumns. Then there is some limited styling you can do depending on the content (e.g. font on Text).

It's important to note the row will not adjust to content.

// For example:
struct ContentView: View {
    @State private var items: [ItemModel] = Array(0...100).map { ItemModel(title: "item \($0)") }
    @State private var selection = Set<ItemModel.ID>()

    var body: some View {
        Table(selection: $selection) {
            TableColumn("title") {
                Text("\($0.title)").padding(.vertical, 16) // <--- HERE
            }
        } rows: {
            ForEach(items) { item in
                TableRow(item)
            }
        }
    }
}
Related