skiptools / skip

Skip transpiler for creating SwiftUI apps for iOS and Android
https://skip.tools
GNU Lesser General Public License v3.0
1.47k stars 39 forks source link

TabView doesn't show label on Android #232

Closed ctopher7 closed 1 week ago

ctopher7 commented 1 week ago

Hi, I'm trying to create a tabview but it doesn't show any label on Android (but I think the background color of the bottom tab is showed) iOS Android my env:

public struct ContentView: View { @State private var selection = 0

private let tabs = [
    BottomTabData(page: AnyView(HomeView()), name: "Home", imageName: "house", imageNameChosen: "house.fill"),
    BottomTabData(page: AnyView(ProductView()), name: "Product", imageName: "tshirt", imageNameChosen: "tshirt.fill"),
    BottomTabData(page: AnyView(UserView()), name: "User", imageName: "person", imageNameChosen: "person.fill"),
    BottomTabData(page: AnyView(OrderView()), name: "Order", imageName: "list.clipboard", imageNameChosen: "list.clipboard.fill"),
    BottomTabData(page: AnyView(VoucherView()), name: "Voucher", imageName: "gift", imageNameChosen: "gift.fill"),
]
public var body: some View{
    TabView(selection: $selection){
        ForEach(Array(tabs.enumerated()), id: \.offset){ idx,data in
            data.page
            .tabItem{
                Label(data.name, systemImage: selection == idx ? data.imageNameChosen : data.imageName)
            }
        }
    }
}

}

fileprivate struct BottomTabData{ let page: T let name: String let imageName: String let imageNameChosen: String }

aabewhite commented 1 week ago

Sorry for the trouble!

When building your code, you get a warning something to the effect of "Skip cannot determine whether this expression results in a View. Consider specifying your types explicitly". That's a hint that the problem is not with the labels, but with Skip's type inference.

If you change the "BottomTabData.page" property to be of type AnyView (removing the generic T), or if in your ForEach you cast to AnyView (i.e. "(data.page as AnyView).tabItem { ... }"), then it works.

Again, apologies for the trouble, but I hope these simple workarounds get you back on track.

ctopher7 commented 1 week ago

@aabewhite your workaround solves the issue, thank you!