在具有全屏覆盖的视图中使用全屏覆盖

Use a Full Screen Cover within a View with a Full Screen Cover

我有一个覆盖整个屏幕的父视图,它有一个 .fullScreenCover。该父视图包含多个子视图,其中包含 'page' 的所有详细信息。其中一个子视图包含一个元素,该元素还具有 .fullScreenCover 控件。

struct ParentView: View {
    @State var isPresent = false

    var body: some View {
        VStack {
            // Other children
            ChildView()
        }.fullScreenCover(isPresent: self.$isPresent) {
            CoverView()
        }
    }
}


struct ChildView: View {
    @State var isPresent = false

    var body: some View {
        Button("Hello", action: { self.isPresent = true })
            .fullScreenView(isPresent: self.$isPresent) {
                ChildCoverView()
            }
    }
}

父视图的屏幕封面按预期打开,但是当我尝试从子视图打开屏幕封面时,没有任何反应。如果我从父视图中删除 .fullScreenCover 控件,那么子屏幕视图就可以工作,所以它必须与嵌套和所有有关。

有没有办法让子 .fullScreenCover 在也有 .fullScreenCover 的父视图中工作?

在 iOS 14.5 beta 3 之前,was/is 有一个错误阻止它工作。

来自发行说明 (https://developer.apple.com/documentation/ios-ipados-release-notes/ios-ipados-14_5-beta-release-notes):

You can now apply multiple sheet(isPresented:onDismiss:content:) and fullScreenCover(item:onDismiss:content:) modifiers in the same view hierarchy. (74246633)

到那时为止的解决方法是从共同祖先呈现或在视图树的不同分支(即不包含子视图的分支)上呈现父 fullScreenCover:

struct ContentView: View {
    @State var isPresent = false

    var body: some View {
        VStack {
            // Other children
            ChildView() //branch 1
            Button("Present Parent") { //branch 2
                isPresent.toggle()
            }.fullScreenCover(isPresented: self.$isPresent) {
                Text("hello")
            }
        }
    }
}


struct ChildView: View {
    @State var isPresent = false

    var body: some View {
        Button("Present child", action: { self.isPresent = true })
            .fullScreenCover(isPresented: self.$isPresent) {
                Text("world")
            }
    }
}