如何将导航按钮添加到SwiftUI应用程序?

如何将导航按钮添加到SwiftUI应用程序?,swift,iphone,xcode,swiftui,navigationview,Swift,Iphone,Xcode,Swiftui,Navigationview,我对Xcode中的Swift和SwiftUI非常陌生,一直在开发一个应用程序 我试图在主屏幕的顶部有三个按钮:主页、关于、帮助 当点击时,它们都会给用户带来不同的视图(或屏幕) 我已经有了创建应用程序的基础:背景、图像、文本、文本字段和按钮(将在稍后设置操作)。我想知道如何将这些按钮添加到我的ZStack(包括VStack和HStack)背景上的应用程序顶部。以下是我目前的代码: import SwiftUI struct ContentView: View { @State

我对Xcode中的Swift和SwiftUI非常陌生,一直在开发一个应用程序

我试图在主屏幕的顶部有三个按钮:主页、关于、帮助

当点击时,它们都会给用户带来不同的视图(或屏幕)

我已经有了创建应用程序的基础:背景、图像、文本、文本字段和按钮(将在稍后设置操作)。我想知道如何将这些按钮添加到我的
ZStack
(包括
VStack
HStack
)背景上的应用程序顶部。以下是我目前的代码:

import SwiftUI

struct ContentView: View {
    
    @State private var accession: String = ""
  
    var body: some View {
     
        ZStack {
            
            // Background navy blue
            Rectangle()
                .foregroundColor(Color(red: 10/255, green: 77/255, blue: 174/255)).edgesIgnoringSafeArea(.all)
        


            // Background light blue
            Rectangle()
                .foregroundColor(Color(red: 86/255, green: 118/255, blue: 255/255))
                .frame(height: 200)
                .offset(x: 0, y: -200)
            
            VStack {
                
                
                // Image of Logo
                Logo()
                    .offset(x: 0, y: -98)
                
                // Text for user input
                Text("Please enter the gene accession number below")
                    .font(.title)
                    .fontWeight(.bold)
                    .foregroundColor(Color.white)
                    .offset(x: -9, y: -10)
                
                // Text field for user input Ω£
                TextField("NG_017013", text: $accession)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .offset(x: 0, y: -8)
                
                // Button
                Button(action: {
                    // ParseXML()
                }) {
                    Text("Find my gene! You can wrap the ZStack in a NavigationView.
At the bottom inside the NavigationView add...

 .navigationBarItems(  
     leading:  
         HStack {  
             NavigationLink(destination: Text("Home View")) {  
                 Text("Home")  
             }  
             NavigationLink(destination: Text("About View")) {  
                 Text("About")  
             }  
         },  
     trailing:  
         NavigationLink(destination: Text("Help View")) {  
             Text("Help")  
         }  
 )  
导入快捷界面
结构ContentView:View{
@国家私有var加入:String=“”
var body:一些观点{
ZStack{
//背景海军蓝
矩形()
.foregroundColor(颜色(红色:10/255,绿色:77/255,蓝色:174/255)).edgesIgnoringSafeArea(.all)
//背景浅蓝色
矩形()
.foregroundColor(颜色(红色:86/255,绿色:118/255,蓝色:255/255))
.框架(高度:200)
.偏移量(x:0,y:-200)
VStack{
//标志图像
Logo()
.偏移量(x:0,y:-98)
//供用户输入的文本
文本(“请在下面输入基因登录号”)
.font(.title)
.fontWeight(.粗体)
.foregroundColor(颜色.白色)
.偏移量(x:-9,y:-10)
//用户输入的文本字段Ω
文本字段(“NG_017013”,文本:$加入)
.textFieldStyle(RoundedBorderTextFieldStyle())
.偏移量(x:0,y:-8)
//钮扣
按钮(操作:{
//ParseXML()
}) {

Text(“查找我的基因!您可以在NavigationView中包装ZStack。
在NavigationView的底部添加


你使用了
navigationview
标签,但从未使用过
navigationview
。你知道你需要什么,快速的谷歌搜索会给你答案。我想这可能就是你正在搜索的方向:嗨,彼得,非常感谢你的帮助!它帮助我获得了应用程序上的按钮!我本应该提到这一点,但我正在尝试当我运行模拟器时,它不是从主页(在Zstack中)开始,而是从显示“帮助视图”的页面开始"好像帮助按钮是自动按下的。如果我按back,而不是返回到Zstack,它只会在屏幕上滑动四分之一。你知道我如何解决这个问题,并让Zstack内容在按下back时占据屏幕并在启动时成为主页面吗?尝试添加
.navigationViewStyle(StackNavigationViewStyle())