带有 tabViewStyle 页面的 SwiftUI TabView 在 iOS 模拟器中运行,但在设备上崩溃

发布于 2025-01-11 04:36:16 字数 2216 浏览 1 评论 0原文

我正在尝试创建一个 SwiftUI iOS 应用程序,其顶层有一个 NavigationLink,还有一个使用 .page 的 TabView 详细视图,这样我就可以在选项卡之间滑动。是的,我知道 TabView 通常位于顶层,带有 NavigationLink 详细信息。

该应用程序在 iOS 模拟器中运行,但在我的 iPhone 上挂起/崩溃。

这是我想要工作的代码:

ContentView.swift

struct ContentView: View {

@State private var selection: String? = nil

var body: some View {
    NavigationView {
        VStack {
            NavigationLink(
                destination: DetailView(),
                tag: "tag1",
                selection: $selection) {
                    Text("Show Detail View")
                        .font(.largeTitle .bold())
                }
        }
    }
}

}

DetailView.swift

struct DetailView: View {

@State private var selectedTab = "one"

var body: some View {
    ZStack {
        // 3 main tabs
        TabView(selection: $selectedTab) {
            Text("Tab 1 View")
                .font(.largeTitle .bold())
                .tag("one")

            Text("Tab 2 View")
                .font(.largeTitle .bold())
                .tag("two")

            Text("Tab 3 View")
                .font(.largeTitle .bold())
                .tag("three")
        }
        // no dots
        .tabViewStyle(.page)
        // .tabViewStyle(PageTabViewStyle())
        // .id(UUID())

        VStack {
            Text("\(selectedTab)")
            Spacer()
        }
    }
}

}

简化的 DetailView 也会以同样的方式崩溃:

SimplifiedDetailView.swift

struct DetailView: View {
var body: some View {
    TabView {
        Text("Tab One")
            .font(.largeTitle .bold())
            .tag("one")

        Text("Tab Two")
            .font(.largeTitle .bold())
            .tag("two")
    }
    // no dots
    .tabViewStyle(.page)
}

当我在iPhone

时,我可以选择 NavigationLink。它变暗然后挂起而不渲染detailView()。很久以后,我收到一个错误对话框,表明该应用程序由于内存问题而终止。

如果我删除 tabViewStyle 修饰符,应用程序将在设备上运行。当然,它默认为传统的选项卡视图,控件位于设备底部。

我在运行 macOS Monterey 12.2.1 (21D62) 的 M1 mac mini 上运行 Xcode 13.2.1 (13C100)。我正在运行 iOS 15.3.1 的 iPhone 8 Plus 上运行。默认情况下,Xcode 的 iOS 部署目标为 iOS 15.2。

有什么建议吗?

I'm trying to create a SwiftUI iOS app with a NavigationLink at the top level, and a TabView detail view, that uses .page, so I can swipe between tabs. And yes, I know the TabView is typically at the top level, with NavigationLink detal.

The app runs in the iOS simulator, but hangs/crashes on my iPhone.

Here is the code I want to work:

ContentView.swift

struct ContentView: View {

@State private var selection: String? = nil

var body: some View {
    NavigationView {
        VStack {
            NavigationLink(
                destination: DetailView(),
                tag: "tag1",
                selection: $selection) {
                    Text("Show Detail View")
                        .font(.largeTitle .bold())
                }
        }
    }
}

}

DetailView.swift

struct DetailView: View {

@State private var selectedTab = "one"

var body: some View {
    ZStack {
        // 3 main tabs
        TabView(selection: $selectedTab) {
            Text("Tab 1 View")
                .font(.largeTitle .bold())
                .tag("one")

            Text("Tab 2 View")
                .font(.largeTitle .bold())
                .tag("two")

            Text("Tab 3 View")
                .font(.largeTitle .bold())
                .tag("three")
        }
        // no dots
        .tabViewStyle(.page)
        // .tabViewStyle(PageTabViewStyle())
        // .id(UUID())

        VStack {
            Text("\(selectedTab)")
            Spacer()
        }
    }
}

}

A simplified DetailView also crahses in the same way:

Simplified detailView.swift

struct DetailView: View {
var body: some View {
    TabView {
        Text("Tab One")
            .font(.largeTitle .bold())
            .tag("one")

        Text("Tab Two")
            .font(.largeTitle .bold())
            .tag("two")
    }
    // no dots
    .tabViewStyle(.page)
}

}

When I run this on my iPhone, I can select the NavigationLink. it dims then hangs without rendering the detailView(). Much later, I get an error dialog, indicating the app was terminated due to a memory issue.

App runs on device if I remove the tabViewStyle modifier. Of course, it defaults to a traditional tab view with controls on the bottom of the device.

I am running Xcode 13.2.1 (13C100) on an M1 mac mini running macOS Monterey 12.2.1 (21D62). I am running on an iPhone 8 Plus running iOS 15.3.1. Xcode, by default, has an iOS Deployment Target of iOS 15.2.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南汐寒笙箫 2025-01-18 04:36:16

使用选择器的示例:

struct DetailView: View {
    @State private var selection: Tab = .tabOne
    enum Tab {
        case tabOne
        case tabTwo
    }

    var body: some View {
        TabView(selection: $selection) {
            Text("Tab One")
                .font(.largeTitle .bold())
                .tag(Tab.tabOne)
            
            Text("Tab Two")
                .font(.largeTitle .bold())
                .tag(Tab.tabTwo)
        }
        // no dots
        .tabViewStyle(.page)
        Picker("Details", selection: $selection) {
            Image(systemName: "1.circle").tag(Tab.tabOne)
            Image(systemName: "2.circle").tag(Tab.tabTwo)
        }
        .pickerStyle(.segmented)
    }
}

Example using a Picker :

struct DetailView: View {
    @State private var selection: Tab = .tabOne
    enum Tab {
        case tabOne
        case tabTwo
    }

    var body: some View {
        TabView(selection: $selection) {
            Text("Tab One")
                .font(.largeTitle .bold())
                .tag(Tab.tabOne)
            
            Text("Tab Two")
                .font(.largeTitle .bold())
                .tag(Tab.tabTwo)
        }
        // no dots
        .tabViewStyle(.page)
        Picker("Details", selection: $selection) {
            Image(systemName: "1.circle").tag(Tab.tabOne)
            Image(systemName: "2.circle").tag(Tab.tabTwo)
        }
        .pickerStyle(.segmented)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文