Binh Ho
Binh Ho

Reputation: 4936

SwiftUI Tabview inside TabView and List items highlight behaviour strange?

I am new in Swift, please help me a hand

The first question is Can use TabView inside a TabView?

If it can be, I got an issue related to List, items inside List is highlighted when I tap into another place

// This tab will have many pages and can swap left/right to change tabs
struct Tab1: View {
    var body: some View {
        TabView(selection: $otherSelected) {
            SubOfTab1()
                .tag("tag1")

            ... other tabs
        }
        .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
    }
}

// This tabview will display tab item as bottom
struct ContentView: View {
    var body: some View {
        TabView(selection: $selected) {
            var body: some View {
                Tab1()
                    .tag("tagParent1")

                ... other tabs
            }
        }
    }
}

struct SubOfTab1: View {
    VStack {
        Text("Try to tab this text")   ------> Try to tab this and all list items below is hightlighted??? why???
            .frame(height: 100)

        List() {
            Text("XX")
            Text("XX")
            Text("XX")
            Text("XX")
            ...
        }
    }
}

First run

enter image description here

Tapped to the text above, all items in List are highlighted?? enter image description here

Upvotes: 1

Views: 759

Answers (1)

IronHead
IronHead

Reputation: 76

I had similar issue with List of .plain style inside a TabView, tapping outside list resulted to selection all visible list items.
Setting .listRowBackground() of list cell fixed it.
In your case:

List() {
        Text("XX")
            .listRowBackground(Color.black)
        Text("XX")
            .listRowBackground(Color.black)
        Text("XX")
            .listRowBackground(Color.black)
        Text("XX")
            .listRowBackground(Color.black)
        ...
}

Upvotes: 2

Related Questions