向 .navigationBarItems 添加退出按钮
add an exit button to .navigationBarItems
我是 swiftui 的初学者。我需要向 .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,Image 和 Text("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后,觉得页面顶部应该也有类似的解决方案
您必须分别将按钮添加到每个子视图。
您应该使用 .toolbar
和 .toolBarItem
,因为 .navigationBarItems
已被弃用。
我是 swiftui 的初学者。我需要向 .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,Image 和 Text("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后,觉得页面顶部应该也有类似的解决方案
您必须分别将按钮添加到每个子视图。
您应该使用 .toolbar
和 .toolBarItem
,因为 .navigationBarItems
已被弃用。