假设我有以下视图...
这将产生一个预期的视图,如下所示: 要向栏中添加导航按钮,可以使用:
@State var tabIndex: Int = 0
var body: some View {
TabView(selection: $tabIndex)
{
Text("Tab 1").tabItem({
Image(systemName: "message")
}).tag(0)
Text("Tab 2").tabItem({
Image(systemName: "arkit")
}).tag(1)
Text("Tab 3").tabItem({
Image(systemName: "battery.100")
}).tag(2)
}.navigationBarTitle("Tabbed View")
}
这将产生一个预期的视图,如下所示: 要向栏中添加导航按钮,可以使用:
.navigationBarItems(trailing:
Button(action: {print("Button was tapped")}) {
Image(systemName: "plus")
.resizable().frame(width: 20, height: 20)
})
如何将按钮添加为预期的那样?
是否有一种方式可以根据条件仅添加(或显示)该按钮?
if (self.tabIndex == 1){
show button
}
else {
don't show button
}
TabView
嵌套在NavigationView
中不是一个好的选择。相反,TabView
中的每个选项卡都应该有自己的NavigationView
。例如,如果有三个选项卡,并且每个选项卡都需要其自己的导航层次结构,则应该有三个NavigationView
。 - nayem