Carousel/sidebar list styles in SwiftUI

Viewed 4648

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.

3 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 others pointed out, the syntax is changing.

Try it this way:

.listStyle(CarouselListStyle())

You're right.

It looks like both .carousel and .sidebar styles are not working / not implemented on iOS.

Here's my demo code:

struct ContentView : View {

    @State var alternateStyle = false

    var body: some View {
        var list  =
        AnyView(List(0...100) { item in
            Text("\(item)").tapAction { self.alternateStyle.toggle() }
        }
        .navigationBarTitle(Text("A List")))
        if alternateStyle {
            list = AnyView(list.listStyle(.carousel))
        } else {
            list = AnyView(list.listStyle(.default))
        }
        return list
    }

}

If you tap on a Text, thus swapping the style, you get a blank list.

SwiftUI is still in beta, hence a lot of components are broken or missing.

Related