PageTabViewStyle 打破了 NavigationView [英] PageTabViewStyle breaks NavigationView

查看:44
本文介绍了PageTabViewStyle 打破了 NavigationView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 SwiftUI 视图.在 TabView 中使用 NavigationView 时,应用程序按预期工作.

I am working on a SwiftUI view. When using the NavigationView inside the TabView the app works as expected.

struct ContentView: View {
    
    var body: some View {
        TabView {
            NavigationView {
                Text("A")
                    .navigationBarTitle("Nav A")
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            NavigationView {
                Text("B")
                    .navigationBarTitle("Nav B")
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
        }
        //.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never)) // this line breaks the view
    }
}

一旦我取消注释 .tabViewStyle(...),视图就无法按预期工作.任何想法为什么?文字不再可见.导航似乎也不正确.

As soon as I uncomment .tabViewStyle(...) the view does not work as expected. Any ideas why? The text is not visible anymore. The navigation does not seem to be correct either.

推荐答案

尝试将 NavigationView 移到 TabView 之外:

Try moving the NavigationView outside the TabView:

struct ContentView: View {
    @State var selection = 1

    var body: some View {
        NavigationView {
            TabView(selection: $selection) {
                Text("A").tag(1)
                Text("B").tag(2)
            }
            .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
            .navigationTitle(selection == 1 ? "A" : "B")
        }
    }
}

这篇关于PageTabViewStyle 打破了 NavigationView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆