简体   繁体   English

NavigationView 的内容未显示 SwiftUI

[英]Content of NavigationView not showing up SwiftUI

I am attempting to create a NavigationView in SwiftUI with a single NavigationLink inside.我试图在 SwiftUI 中创建一个 NavigationView,里面有一个 NavigationLink。 My code so far is:到目前为止我的代码是:

import SwiftUI

struct HomeView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: PlayersView()) {
                Text("Players")
                    .foregroundColor(.white)
                    .padding(12)

            }
            .background(Color.black)
            .cornerRadius(12)


            .navigationBarTitle(Text("Home"))
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        HomeView()
    }
}

Nothing gets displayed in the live preview or when I run on a simulator.实时预览中或在模拟器上运行时不会显示任何内容。 The live preview looks like this:实时预览如下所示:

SwiftUI 实时预览示例 1

If I change the code a little bit and duplicate the NavigationLink then I can get one of the NavigationLinks to appear.如果我稍微更改代码并复制 NavigationLink,则可以显示其中一个 NavigationLink。

struct HomeView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: PlayersView()) {
                Text("Players")
                    .foregroundColor(.white)
                    .padding(12)

            }
            .background(Color.black)
            .cornerRadius(12)

            NavigationLink(destination: PlayersView()) {
                Text("Players")
                    .foregroundColor(.white)
                    .padding(12)

            }
            .background(Color.black)
            .cornerRadius(12)

            .navigationBarTitle(Text("Home"))
        }
    }
}

SwiftUI 实时预览示例 2

I am trying to make a home page with multiple links to other SwiftUI screen view files.我正在尝试制作一个主页,其中包含指向其他 SwiftUI 屏幕视图文件的多个链接。 Any help is appreciated!任何帮助表示赞赏!

Thanks.谢谢。

Instead of this而不是这个

NavigationLink(destination: PlayersView()) {
    Text("Players")
        .foregroundColor(.white)
        .padding(12)

}
.background(Color.black)
.cornerRadius(12)

do this做这个

NavigationLink(destination: PlayersView()) {
    Text("Players")
        .foregroundColor(.white)
        .padding(12)
        .background(Color.black)
        .cornerRadius(12)
}

Update : I assume I've got your issue, which I at first just passed automatically.更新:我假设我遇到了您的问题,起初我只是自动通过了。 NavigationView is platform specific. NavigationView 是特定于平台的。 By default iPad has Master/Details navigation style, which has different presentation depending on orientation, so you don't see in Preview your link, because Preview is in Portrait and navigation link is in Sidebar, which is hidden.默认情况下,iPad 具有主/详细信息导航样式,根据方向具有不同的呈现方式,因此您在预览中看不到链接,因为预览是纵向的,而导航链接在侧边栏(隐藏)中。

If you want to see it in such Preview, you need to use .navigationViewStyle(StackNavigationViewStyle()) for NavigationView .如果你想在这样的预览中看到它,你需要为NavigationView使用.navigationViewStyle(StackNavigationViewStyle()) Or, in your variant, just run application in Simulator and using CMD-Left/Right arrow rotate Simulator.或者,在您的变体中,只需在模拟器中运行应用程序并使用CMD-Left/Right arrow旋转模拟器。

Note : It's not possible to rotate Preview or now.注意:无法旋转预览或现在。 Just in case.以防万一。

如果您使用的是 iPad,我想说第一个navigationLink用作草图/索引视图,您可以通过向右滑动将其取出。

Try this, I am also having issues initially but trying this way works for me !!试试这个,我最初也有问题,但尝试这种方式对我有用!!

  var body: some View 
  {
      NavigationView 
     {
           VStack(alignment: .center)
              {
                  //Your ui designs
              } 
       .navigationBarTitle(Text("Forgot 
        Password").font(.largeTitle).fontWeight(.medium), displayMode: .inline)

      }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM