admin管理员组

文章数量:1129451

I want to descrease row height of list with the following code:

List(elements, id: \.id, children: \.children, selection: $selected) { element in
    HStack {
        Image(systemName: element.children != nil ? "folder" : "list.bullet.rectangle.portrait")
        Text(element.name)
    }
    .padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
    .listRowSeparator(.hidden, edges: .all)
    .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
}
.environment(\.defaultMinListRowHeight, 20)
.listStyle(.inset)

The UI looks like:

But I want the background to be blur. After I change .listStyle(.inset) to .listStyle(.sidebar), the row height is increased.

No matter how I lower the paddings or insets of list elements, the row height is not decreased. I found that XCode has did it.

But I cannot find a way to make it.

Environment:

  • macOS 15.2
  • XCode 16.2

I want to descrease row height of list with the following code:

List(elements, id: \.id, children: \.children, selection: $selected) { element in
    HStack {
        Image(systemName: element.children != nil ? "folder" : "list.bullet.rectangle.portrait")
        Text(element.name)
    }
    .padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
    .listRowSeparator(.hidden, edges: .all)
    .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
}
.environment(\.defaultMinListRowHeight, 20)
.listStyle(.inset)

The UI looks like:

But I want the background to be blur. After I change .listStyle(.inset) to .listStyle(.sidebar), the row height is increased.

No matter how I lower the paddings or insets of list elements, the row height is not decreased. I found that XCode has did it.

But I cannot find a way to make it.

Environment:

  • macOS 15.2
  • XCode 16.2
Share Improve this question asked Jan 8 at 9:25 rackrdrackrd 4558 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Some parts of SwiftUI list styles just cannot be customised. I'd suggest keep using the .inset style, and add a NSVisualEffectView as the background manually.

struct BackgroundBlur : NSViewRepresentable {
    func makeNSView(context: Context) -> NSVisualEffectView {
        let view = NSVisualEffectView()
        view.state = .active
        return view
    }
    func updateNSView(_ view: NSVisualEffectView, context: Context) { }
}
.listStyle(.inset)
.scrollContentBackground(.hidden) // remember to hide the default background or else our own background will not be visible
.background {
    BackgroundBlur().ignoresSafeArea()
}

本文标签: How to decrease row height of list in sidebar style on swiftui under macosStack Overflow