Search code examples
swiftuiswiftui-tabviewswiftui-sheet

Presenting Sheet reset the Selected Tab (SwiftUI)


Presenting sheet from childView of tabItem rerendering the whole Windows Group. My TabView alrdy has a selection Binding. When I use Fullscreen Cover it work fine. and sheet work fine with NavigationSplitView on iPad too. I have Attached some of the snippets. Please give some Ideas.

.sheet(item: $selectedVoucher) {
// on dismiss action 
} content: { voucher in
VoucherDetailsView(voucher: voucher)
}

Here is my TabView

TabView(selection: $selection) {
ForEach([NavigationItem.home, NavigationItem.vehicles, NavigationItem.workshop, NavigationItem.wallet, NavigationItem.profile]) { item in
item.destination
.tabItem { item.label }
.id(item)
.tag(item)
}
}

My Child View is wrapped in NavigationStack

The result I want is Just show the sheet only , not re-render the whole WindowGroup just bcos sheet is shown. .fullScreenCover is working fine not messing up with the TabView.


Solution

  • $selection must be binded to some persistent object.

    I don't see your full code, but try to update it as follows:

    Instead of @State var selection = … use:

    @StateObject var model = ViewModel()
    
    class ViewModel: ObservableObject {
        @Published var selection = ...
    }
    …
    TabView(selection: $model.selection) {
       ...
    }