下面的代码没有显示Text("Second View")
,因为private var selection: UInt
。
struct TabMenu: View {
@State private var selection: UInt = 0
var body: some View {
TabView(selection: $selection){
Text("First View")
.font(.title)
.tabItem {
Image(systemName: "house")
Text("Main")
}
.tag(0)
Text("Second View")
.font(.title)
.tabItem {
Image(systemName: "gear")
Text("Preferences")
}
.tag(1)
}
}
}
在TabView的源代码中,我发现了这一点:
extension TabView where SelectionValue == Int {
@available(watchOS, unavailable)
public init(@ViewBuilder content: () -> Content)
}
如何在SelectionValue == Uint
上创建自定义视图?
where SelectionValue == Uint
答案 0 :(得分:1)
选择的类型必须与标记的类型相同(默认情况下均为Int)。
这是一个解决方案。使用Xcode 11.4 / iOS 13.4进行了测试
struct TabMenu: View {
@State private var selection: UInt = 0
var body: some View {
TabView(selection: $selection){
Text("First View")
.font(.title)
.tabItem {
Image(systemName: "house")
Text("Main")
}
.tag(UInt(0))
Text("Second View")
.font(.title)
.tabItem {
Image(systemName: "gear")
Text("Preferences")
}
.tag(UInt(1))
}
}
}