我是斯提图伊的初学者。我需要向.navigationBarItems添加一个退出按钮。如何在父NavigationView中添加此按钮以在所有子视图上显示此按钮?
//我问题上的一个简单例子
struct FirstView: View {
var body: some View {
NavigationView {
ZStack{
TabView{
SubExampleViewOne()
.tabItem {
Image(systemName: "house.fill")
Text("Home")
}
SubExampleViewTwo()
.tabItem {
Image(systemName: "bookmark.circle.fill")
Text("Bookmark")
}
}
}
//here I have added a toolbar and it is perfectly visible in tabitem
//this is what I am trying to achieve, the visibility of the button on all pages
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
ButtonExitView()
}
}
}
}
}奇怪的是--如果我以这种方式添加NavigationLink,图像和文本(“Home”)就会出现两次,并且ToolbarItem不再出现在新页面上
struct SubExampleViewOne: View {
var body: some View {
Text("This is hime page!")
.padding()
NavigationLink(destination: SubExampleViewThree()){
Text("Navigation link")
}
}
}
struct SubExampleViewTwo: View {
var body: some View {
Text("Hello, world!")
.padding()
}
}
struct SubExampleViewThree: View {
var body: some View {
Text("This is Navigation link")
.padding()
}
}
struct ButtonExitView: View {
var body: some View {
Button(action: {}, label: {Image(systemName: "arrowshape.turn.up.right.circle")})
}
}在了解了TabView之后,我认为页面顶部应该有类似的解决方案。
发布于 2022-03-11 18:21:26
您必须将按钮分别添加到每个子视图中。
您应该使用.toolbar和.toolBarItem,因为不推荐使用.navigationBarItems。
https://stackoverflow.com/questions/71432305
复制相似问题