Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a list with headers using SwiftUI and macOS

I like to build an old styled macOS app, not an iOS app! Therefore I need a list with a light grey background header row with the titles of the column. Usualy the user can resize each column by dragging the end of the header column around. But how can I set it with SwiftUI?

I read a lot about iOS and manually setting some text (titles/headers) above. But the intention is to use a build-in functionality, that this title belongs to a column and when changing the width of the title, the column is also changed. Also a double click will sort the entire list accordingly to the clicked column up or down. The width of the columns will also automatically fit the content.

Where is the property to set all these features?

like image 434
Peter71 Avatar asked Oct 20 '25 03:10

Peter71


1 Answers

SwiftUI lists can have only one column. Xcode 13 adds a Table control for Mac apps. The Table control supports multiple columns, but it requires macOS 12.

To support earlier macOS versions, you can wrap AppKit views, such as NSTableView, in SwiftUI by creating a struct that conforms to NSViewRepresentable. Wrapping NSTableView should give you the Mac table view behavior you want.

SwiftUI does support Mac lists having the sidebar list style. Use SidebarListStyle as the list's style.

.listStyle(SidebarListStyle())
like image 124
Swift Dev Journal Avatar answered Oct 21 '25 22:10

Swift Dev Journal



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!