Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carousel/sidebar list styles in SwiftUI

There are two list styles that are inactive and produce no result with SwiftUI. Try the .carousel and .sidebar styles to figure it out.

struct ContentView : View {

    var body: some View {
        List {
            Text("Hello")
        }
        .listStyle(.carousel) // Or .sidebar
    }

}

Is this due to the beta (bug), or did I missed something ? The List doesn't appear on my iPhone.

like image 561
Benjamin Pisano Avatar asked Nov 17 '25 09:11

Benjamin Pisano


2 Answers

SidebarListStyle is available on macOS only, as CarouselListStyle is only for watchOS.

Here's what sidebar-style list looks like when running on macOS:

Expanded:

Expanded

Collapsed:

Collapsed

Here's the code (although I'm not sure that an instance of Range or any other one-dimensional sequence is the right choice for this style):

struct ContentView : View {
    var body: some View {
        List (0..<10) { number in
            self.view(forNumber: number)
            }
            .listStyle(.sidebar)
    }

    func view(forNumber number: Int) -> some View {
        print("number == \(number)")
        let ret = Text("#\(number)")
            .foregroundColor(.blue)
        return ret
    }
}
like image 150
Russian Avatar answered Nov 20 '25 04:11

Russian


Like others pointed out, the syntax is changing.

Try it this way:

.listStyle(CarouselListStyle())
like image 28
Khanan Grauer Avatar answered Nov 20 '25 02:11

Khanan Grauer