[英]Navigation View moved my destination view 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)
}
}
由於缺少許多依賴組件,提供的代碼不可測試,但我建議刪除硬編碼的幀(因為它們使視圖不靈活)並僅使用堆棧/對齊/填充重新布局。
}.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) // !!
在某些情況下,您必須使視圖的框架不是解決這個問題的核心,但在我的情況下,問題是我添加了兩次導航視圖,所以我遇到了這個問題。
我在輔助視圖中刪除了導航視圖,這完全解決了我的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.