【问题标题】:Navigation View moved my destination view SwiftUI导航视图移动了我的目标视图 SwiftUI
【发布时间】:2021-02-07 09:47:03
【问题描述】:

您好,我是 swiftUI 的新手。我正在使用导航视图通过导航链接在我的视图之间创建导航,我发现了一个问题,我不知道这是我的代码中的错误或错误 第一个图像是我的视图在我的子视图文件中看起来像这样,第二个当我在导航中使用它时它会像这样自动移动

这是我在父母视图中的代码

struct OnboardingSet: View {
    
    private var offset : CGFloat = screenFrame.Width
    @State private var currentPage : Int = 0
    @State private var NavigationTag : Int? = nil
    
    var body: some View {
        NavigationView {
            
            NavigationLink(
                destination: SigninView(),
                tag: 1,
                selection: $NavigationTag,
                label: {
                    
                    HStack {
                        OnboardingView(ImageName: Resource.Image.onboarding1, Title: "First, Truth, \nPopular Topic", ButtonTitle: "Skip", TotalPage: 3, CurrentPage: 1) {
                            NavigationTag = 1
                        }
                        OnboardingView(ImageName: Resource.Image.onboarding2, Title: "Fast, Secure, \nMost Loved By User", ButtonTitle: "Skip", TotalPage: 3, CurrentPage: 2) {
                            NavigationTag = 1
                        }
                        OnboardingView(ImageName: Resource.Image.onboarding3, Title: "Feel the \n happiness", ButtonTitle: "sign in", TotalPage: 3, CurrentPage: 3) {
                            NavigationTag = 1
                        }
                    }
                    .offset(x: currentPage == 0 ? offset : 0, y: 0)
                    .offset(x: currentPage == 2 ? -offset : 0, y: 0)
                    .animation(.easeInOut)
                    .gesture(
                        DragGesture(minimumDistance: 5, coordinateSpace: .local)
                            .onEnded({ (value) in
                                if value.translation.width < 0 {
                                    if currentPage != 2 {
                                        currentPage += 1
                                    }
                                }
                                
                                if value.translation.width > 0 {
                                    if currentPage != 0 {
                                        currentPage -= 1
                                    }
                                }
                            })
                    )
                })
        }
    }
    
}

这是我的子视图

struct SigninView: View {
    
    @ObservedObject private var viewModal = SignInViewModal()
    
    var body: some View {
        ZStack {
            Image(Resource.Image.backgroundSignIn)
                .resizable()
                .aspectRatio(contentMode: .fill)
                .blur(radius: 4)
                .offset(x: 200, y: 10.0)
            
            VStack {
                HStack {
                    Text("Hello Unwary \n Reader")
                        .foregroundColor(.white)
                        .font(.system(size: 42))
                        .fontWeight(.black)
                        .padding(.top,64)
                        .padding(.leading)
                        .shadow(color: .black, radius: 16, x: 16, y: 16)
                    Spacer()
                }
                Spacer()
                
                VStack {
                    BlurTextFeild("Email", Text: $viewModal.Email, TextEntryType: .Open)
                        .frame(width: screenFrame.Width - 30, height: 100)
                    
                    BlurTextFeild("Password", Text: $viewModal.Password,TextEntryType: .Secure)
                        .frame(width: screenFrame.Width - 30, height: 100)
                    
                }.padding()
                
                Button(action: {
                    viewModal.AuthState = viewModal.AuthState == SignInViewModal.authState.signIn ? SignInViewModal.authState.signUp : SignInViewModal.authState.signIn
                }, label: {
                    Text(viewModal.AuthState == SignInViewModal.authState.signIn ? "Create Account" : "LogIn")
                        .foregroundColor(.black)
                        .opacity(0.5)
                        .font(.system(size: 18, weight: .bold, design: .rounded))
                })
                
                Spacer()
                
                Button(action: {
                    
                }, label: {
                    ZStack {
                        
                        Blur(effect: UIBlurEffect(style: .regular))
                        
                        Color.black
                            .opacity(0.3)
                        
                        Text(viewModal.AuthState == SignInViewModal.authState.signIn ? "Sign In" : "Sign Up")
                            .foregroundColor(.white)
                            .font(.system(size: 24))
                    }.frame(width: screenFrame.Width - 100, height: 80)
                    .cornerRadius(16)
                }).padding(.bottom,32)
            }.frame(width: screenFrame.Width, height: screenFrame.Height)
        }
        .edgesIgnoringSafeArea(.all)
        .navigationBarBackButtonHidden(true)
        .frame(width: screenFrame.Width, height: screenFrame.Height + 50, alignment: .center)
    }
}

提前谢谢你

【问题讨论】:

    标签: ios swift swiftui navigation swiftui-navigationlink


    【解决方案1】:

    由于缺少许多依赖组件,所提供的代码不可测试,但我建议删除硬编码的帧(因为它们使视图不灵活)并仅使用堆栈/对齐/填充重新布局。

                }.frame(width: screenFrame.Width - 100, height: 80)    // !!
                .cornerRadius(16)
            }).padding(.bottom,32)
        }.frame(width: screenFrame.Width, height: screenFrame.Height)   // !!
    }
    .edgesIgnoringSafeArea(.all)
    .navigationBarBackButtonHidden(true)
    .frame(width: screenFrame.Width, height: screenFrame.Height + 50, alignment: .center)  // !!
    

    【讨论】:

    • 是的,它对我有用,我弄清楚什么是不需要的,然后删除它对我有用的框架,谢谢!!
    【解决方案2】:

    在某些情况下,您必须使视图的框架不是解决此问题的核心,但在我的情况下,问题是我添加了两次导航视图,所以我遇到了这个问题

    我在二级视图中删除导航视图,它完全解决了我的问题

    【讨论】:

      猜你喜欢
      • 2015-01-23
      • 2022-06-19
      • 1970-01-01
      • 2021-05-26
      • 2020-01-10
      • 2019-11-30
      • 2018-08-03
      • 2015-04-22
      • 2021-12-30
      相关资源
      最近更新 更多