SwiftUI - 在包含 .toolbar

时间:2021-04-02 09:07:41

标签: ios swift swiftui

在某些条件下展示工作表时,我遇到了奇怪的崩溃。我已经能够在入门项目中重现它,并且已经从多个人那里得到确认,它最终也会崩溃。奇怪的是,对于某些模拟器/用户,这些步骤不会使应用程序崩溃。

我已经向 Apple (FB9064549) 提交了错误报告,但我想我也会在这里询问。

重现崩溃的步骤如下:

  1. 点击“打开详细信息视图”按钮
  2. 回去
  3. 再次单击“打开详细信息视图”按钮
  4. 点击“当前模式”
  5. 它会崩溃

基本内容视图有一个工具栏,里面有一个菜单(或只是一个普通的文本)和一个导航链接,用于推送到一个新页面。如果我注释掉 .toolbar,则不会发生崩溃。

struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack {
                 NavigationLink(
                     destination: DetailView(),
                     label: {
                         Text("Open Detail View")
                     })
            }
                
                // If you comment this out, it does not crash
                .toolbar {
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Menu {
                            ForEach(1...5, id: \.self) { index in
                                Button {
                                    print("tap menu item")
                                } label: {
                                    HStack {
                                        Text("Menu Item \(index)")
                                    }
                                }
                            }
                        } label: {
                            Text("Filter")
                        }
                    }
                }
        }
    }
}

呈现的DetailView可以通过绑定项呈现一个.sheet。如果我改用 FullScreenCover,它不会崩溃。

struct DetailView: View {
    @State var modalType: ModalType?
    var body: some View {
        Button(action: {
            modalType = .modalWithTabView
        }, label: {
            Text("Present Modal")
        })
        .sheet(item: $modalType, content: { $0 })
    }
}

enum ModalType: Identifiable, View {
    case modalWithTabView

    var id: String {
        return "modalWithTabView"
    }

    var body: some View {
        switch self {
            case .modalWithTabView:
                ModalWithTabView()
        }
    }
}

显示的工作表是一个基本的 TabView 分页。如果我不添加 PageTabViewStyle 样式,它不会崩溃。

struct ModalWithTabView: View {
    @State var currentStep = 0
    var body: some View {
        TabView(selection: $currentStep) {
            ForEach (0 ..< 10) { index in
                Text("Page \(index)")
                    .tag(index)
            }
        }
        // If you comment this out, it does not crash
        .tabViewStyle(PageTabViewStyle())
    }
}

如果有人有任何指点,请告诉我!

0 个答案:

没有答案
相关问题