ios – Using SwiftUIs horizontal tabs with a sheet in on a screen: “Presenting view controller…from detached view controller…is not supported, and…”


I’m working on a mobile design where there are 3 tabs layed out horizontally. The middle screen is the main tab. This middle screen has a sheet that pops up to show some content to the user.

The sheet needs to stay visible in its smallest height at all times. But, when my app first loads I get this warning in the console:

Presenting view controller <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x10804e000> from detached view controller <TtGC7SwiftUI32NavigationStackHostingControllerVS_7AnyView: 0x106843200> is not supported, and may result in incorrect safe area insets and a corrupt root presentation. Make sure <TtGC7SwiftUI32NavigationStackHostingControllerVS_7AnyView: 0x106843200> is in the view controller hierarchy before presenting from it. Will become a hard exception in a future release.

This warning also shows up when swiping away from the middle screen and back into the center screen. What are my options to fix this issue?

This is the code for the setup:

struct HorizontalTabs: View {
    enum Tab {
        case one, two, three
    }
    
    @State private var showSheet = true
    @State private var selectedTab: Tab = .two
    
    var body: some View {
        TabView(selection: $selectedTab) {
            NavigationStack {
                Screen1()
            }
            .tag(Tab.one)
            
            NavigationStack {
                Screen2(showSheet: $showSheet)
            }
            .tag(Tab.two)
            
            NavigationStack {
                Screen3()
            }
            .tag(Tab.three)
        }
        .onChange(of: selectedTab, initial: true) { _, newTab in
            if newTab != .two {
                showSheet = false
            } else {
                showSheet = true
            }
        }
        .tabViewStyle(.page(indexDisplayMode: .never))
    }
}
struct Screen2: View {
    
    @Binding var showSheet: Bool
    
    var body: some View {
        VStack {
            Text("Screen 2")
        }
        .sheet(isPresented: $showSheet) {
            Text("Sheet Content")
                .interactiveDismissDisabled()
                .presentationDragIndicator(.visible)
                .presentationDetents([.fraction(0.1), .large])
                .presentationBackgroundInteraction(.enabled)
                .presentationBackground(.background)
        }
        .navigationTitle("Screen 2")
        .navigationBarTitleDisplayMode(.inline)
    }
}

Screen 1 and 2 are just blank SwiftUI screens.

I’m not sure where to go from here. It says this warning will become a hard exception in the futuer so I want to get ahead of that. Any guidance on this would be much appreciated!

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img