简体   繁体   English

SwiftUI canvas 预览为一个视图显示多个视图

[英]SwiftUI canvas preview displaying multiple views for one view

My SwiftUI preview is showing one view as 3 different preview screens and it should be one screen with the combined views...我的 SwiftUI 预览将一个视图显示为 3 个不同的预览屏幕,它应该是一个具有组合视图的屏幕...

What am I doing wrong?我究竟做错了什么?

Thank you!谢谢!

See images attached of 3 separate preview views.请参阅附加的 3 个单独预览视图的图像。 在此处输入图像描述

struct SignupSelfie: View {
    @ObservedObject var signupImagesViewModel = SignupNavigationViewModel()
    @State private var isValid = false
        
    var body: some View {
      
        VStack {
           Button("capture") { /* action code here */ }
              .background(
                  NavigationLink("", destination: SignupIdView(), isActive: $isValid)
                )
            }.navigationBarBackButtonHidden(true)
            Spacer()
            Text("capture selfie")
        }
}


struct SignupSelfie_Previews: PreviewProvider {
    static var previews: some View {
        SignupSelfie()
    }
}

SwiftUI previews will display a preview for each node at the root of the view tree. SwiftUI 预览将在视图树的根部显示每个节点的预览。 This is more clear when it's inside the preview itself, like:当它在预览本身内时,这会更清楚,例如:

struct SignupSelfie_Previews: PreviewProvider {
    static var previews: some View {
        SignupSelfie()
        SignupSelfie2()
        SignupSelfie3()
    }
}

In the above, it's clear why there would be 3 previews.在上面,很清楚为什么会有 3 个预览。

In your example, as a commenter alluded to, it's happening in your SignupSelfie because you have 3 nodes at the root:在您的示例中,正如评论者所暗示的那样,它发生在您的SignupSelfie中,因为您在根目录中有 3 个节点:

  1. VStack
  2. Spacer()
  3. Text("capture selfie")

My guess is the indentation you have may have made it look like the Spacer and Text were inside the VStack -- a good trick is to select your code and use Ctrl-i to have Xcode auto-format -- it'll fix indentation issues like this and reveal the issue.我的猜测是你的缩进可能使它看起来像SpacerTextVStack ——一个好技巧是 select 你的代码并使用Ctrl-i让 Xcode 自动格式化——它会解决缩进问题像这样并揭示问题。

In this case, you'll probably just want to put everything inside the VStack .在这种情况下,您可能只想将所有内容都放在VStack中。 In other cases, it's good to know that you can also avoid this issue and turn multiple nodes into one by wrapping them in Group {}在其他情况下,很高兴知道您也可以通过将多个节点包装在Group {}中来避免此问题并将多个节点合二为一

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

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