'SwiftUI MacOS Inset paddings of List
i have a scrollale list view using SwiftUI on a macOS app and i'm trying to add paddings inside the scrollable area, without success. Here my code:
List(appListItems, id: \.self, selection: $selectedItem) { app in
ApplistItem(item: app, selected: selectedItem?.hashValue == app.hashValue)
}
.background(Color.red)
.padding(24)
but what i get is this:
What the design requires is this, as you can see i want the scrollbar to stay on the right edge and the top/bottom distance should be included in the scroll area, and not put outside it. The horizontal paddings are not required, but i can't figure out how to handle top and bottom paddings and keep them inside the scroll area
Solution 1:[1]
Just solved by adding Spacer
before and after the loop.
Solution 2:[2]
You need list row insets, but it is applied to dynamic content, so you need to use ForEach
, like
List {
ForEach(appListItems, id: \.self, selection: $selectedItem) { app in
ApplistItem(item: app, selected: selectedItem?.hashValue == app.hashValue)
}
.listRowInsets(EdgeInsets(top: 0, leading: 24, bottom: 0, trailing: 24))
}
.background(Color.red)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mattia Astorino |
Solution 2 | Asperi |