简体   繁体   English

SwiftUI NavigationView + tabView 显示导航标题略微偏离屏幕

[英]SwiftUI NavigationView + tabView shows navigation title slightly off the screen

I have three views inside of TabView, each with a naviagtionTitle.我在 TabView 中有三个视图,每个视图都有一个 naviagtionTitle。 However, when the first view shows up, the navigationTitle leads almost off the screen.然而,当第一个视图出现时,navigationTitle 几乎离开了屏幕。 It is only when I click to another page and then back to the original that the navigationTitle appears in the correct placement.只有当我单击另一个页面然后返回到原始页面时,navigationTitle 才会出现在正确的位置。 What could be the problem?可能是什么问题呢?

struct OnboardingView : View {
    
    @Binding var shouldShowOnboarding: Bool
    
    
    var body : some View {
        TabView {
            PageView(imageName: "Onboarding",
                     title: "Zone Out of This World",
                     showsDismissButton: false,
                     shouldShowOnboarding : $shouldShowOnboarding)
            PageView(imageName: "Onboarding",
                     title: "Improve Your Focus",
                     showsDismissButton: false,
                     shouldShowOnboarding : $shouldShowOnboarding)
            PageView(imageName: "Onboarding",
                     title: "Meditate to Relax",
                     showsDismissButton: true,
                     shouldShowOnboarding : $shouldShowOnboarding)
            
        }
        .tabViewStyle(PageTabViewStyle())
        .edgesIgnoringSafeArea(.all)
    }
}



struct PageView : View {
    
    let imageName: String
    let title: String
    let showsDismissButton : Bool
    @Binding var shouldShowOnboarding: Bool
    
    
    
    var body: some View {
        
        NavigationView {
            ZStack {
                Image(imageName)
                    .ignoresSafeArea()
                
                
                if showsDismissButton {
                    Button(action: {
                        shouldShowOnboarding.toggle()


                    }, label: {
                        Text("Get Started")
                            .frame(width: 352, height: 57)
                            .background(Color(#colorLiteral(red: 0.8470588326454163, green: 0.37254902720451355, blue: 0.27450981736183167, alpha: 1)))
                            .foregroundColor(Color.white)
                            .cornerRadius(30)
                            .padding(.top, 600)
                    })
                }
            }
            .navigationTitle(title)
          
        }
        .navigationViewStyle(.stack)
    }
}

I have included screenshots.我已经包括了截图。

Incorrect placement:放置错误: 在此处输入图像描述

Correct placement:正确放置: 在此处输入图像描述

.edgesIgnoringSafeArea(.all) appears to be causing the initial shift. .edgesIgnoringSafeArea(.all)似乎导致了初始转变。 I'm not sure why it corrects itself after you shift views, but based on the current code, I don't see why you want to have the .edgesIgnoringSafeArea so I think it would be safe to just comment it out in order to have the second intended style.我不确定为什么它会在您转移视图后自行纠正,但根据当前代码,我不明白您为什么想要拥有.edgesIgnoringSafeArea所以我认为只需将其注释掉以便拥有第二种预期风格。

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

相关问题 SwiftUI - 在 NavigationView 中嵌套 TabView 时不显示导航栏标题 - SwiftUI - Navigation bar title not displayed when nesting TabView in NavigationView SwiftUI TabView 不起作用,它只是在屏幕外显示文本 - SwiftUI TabView not working, it just shows text off screen NavigationView 内的 SwiftUI TabView - SwiftUI TabView inside a NavigationView 当视图位于 SwiftUI 的 TabView 中时,NavigationView 标题不会出现 - NavigationView title doesn't appear when the views are in TabView in SwiftUI SwiftUI,将标题设置为 NavigationView 内 TabView 的子视图不起作用 - SwiftUI, setting title to child views of TabView inside of NavigationView does not work NavigationView 分层堆栈中的 SwiftUI TabView 在屏幕顶部有额外的空间 - SwiftUI TabView in NavigationView hierarchical stack has extra space at the top of the screen SwiftUI:NavigationView 中列表的导航标题卡在原位(滚动动画损坏) - SwiftUI: Navigation Title Stuck in Place (Scroll Animation Broken) for List in NavigationView SwiftUI 中的自定义 TabView 导航 - Custom TabView navigation in SwiftUI SwiftUI - 带有 NavigationView 的 TabView 生成灰色区域 - SwiftUI - TabView with NavigationView generates gray area 如何在 SwiftUI 中使用 NavigationView 创建自定义 TabView? - How to create a custom TabView with NavigationView in SwiftUI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM