I have taken a long-ish break from 100 Days of SwiftUI to do a couple of related things. I am trying to really nail done SwiftData so I am doing the SwiftData by Example Collection. Also, using what I have learned, thus far, I have created an app from a long languishing idea that has been waiting for me to turn it into an app on the AppStore. I completed the business logic two years ago, but needed the time and SwiftUI experience to make it happen in a way that would make me happy. It is in Test Flight now. Every part of creating the app just flowed. I knew what I wanted to do. I knew how to execute each part of the app or knew where to get the info. For this app, I had reached critical mass in my progress and I needed to create it. I'll get back to learning SwiftUI in a week.
One more thing I discovered while doing this new app was how to get the Section Header to stay on the screen when scrolling the contents of the section. Much like the secret of how to get buttons to recognize button taps when they are in a list row, you just had to somehow know or find the secret. It was not discoverable.
var body: some View {
List {
Section(header: SectionHeaderView(total: users.count)) {
ForEach(users) { user in
ListRowView(user: user)
}
.onDelete() { indexes in
users.remove(atOffsets: indexes)
}
}
}
.listStyle(.plain)
}
}
Again, it was changing the style of a UI View. Here, in order for the Section Header to be "sticky" and stay in view which scrolling the list up, the style of the list had be .listStyle(.plain).
Why this is? I believe it is because of a complicated interaction with the default .insetGrouped listStyle support of sidebars on iPads and Macs. That's all I know. You need to apply this one non-default listStyle and then it works. Magic.