SwiftUI禁用TabView中的特定tabItem选择吗?

问题描述 投票:0回答:1

我有一个TabView,在点击[+](第二个)tabItem后显示一张纸。同时,ContentView也在切换TabView的选项卡选择,因此当我关闭显示的工作表时,所选的选项卡是空白的,没有任何内容。并非理想的用户体验。

我的问题:

[我想知道如何才能简单地禁用特定的tabItem,使其不像一个选项卡那样,而只是在显示该表的同时保持在点击[+]项之前的选项卡选择。 SwiftUI可能实现这种效果,还是我应该通过另一种方式来实现这种效果?

我的标签栏的图像:

enter image description here

这是我的ContentView的代码,其中我的TabView是:

struct SheetPresenter<Content>: View where Content: View {
    @EnvironmentObject var appState: AppState
    @Binding var isPresenting: Bool

    var content: Content
    var body: some View {
        Text("")
            .sheet(isPresented: self.$isPresenting, onDismiss: {
                // change back to previous tab selection
                print("New listing sheet was dismissed")

            }, content: { self.content})
            .onAppear {
                DispatchQueue.main.async {
                    self.isPresenting = true
                    print("New listing sheet appeared with previous tab as tab \(self.appState.selectedTab).")
                }
            }
    }
}

struct ContentView: View {
    @EnvironmentObject var appState: AppState
    @State private var selection = 0
    @State var newListingPresented = false

    var body: some View {

        $appState.selectedTab back to just '$selection'
        TabView(selection: $appState.selectedTab){

            // Browse
            BrowseView()
                .tabItem {
                    Image(systemName: (selection == 0 ? "square.grid.2x2.fill" : "square.grid.2x2")).font(.system(size: 22))
                }
                .tag(0)


            // New Listing
            SheetPresenter(isPresenting: $newListingPresented, content: NewListingView(isPresented: self.$newListingPresented))
                .tabItem {
                    Image(systemName: "plus.square").font(.system(size: 22))
                }
                .tag(1)

            // Bag
            BagView()
                .tabItem {
                    Image(systemName: (selection == 2 ? "bag.fill" : "bag")).font(.system(size: 22))
                }
                .tag(2)

            // Profile
            ProfileView()
                .tabItem {
                    Image(systemName: (selection == 3 ? "person.crop.square.fill" : "person.crop.square")).font(.system(size: 22))
                }
                .tag(3)
        }.edgesIgnoringSafeArea(.top)
    }
}

这里是AppState

final class AppState: ObservableObject {

    @Published var selectedTab: Int = 0
}
ios swift swiftui tabview tabitem
1个回答
0
投票
struct SheetPresenter<Content>: View where Content: View { @EnvironmentObject var appState: AppState @Binding var isPresenting: Bool @Binding var showOtherTab: Int var content: Content var body: some View { Text("") .sheet(isPresented: self.$isPresenting, onDismiss: { // change back to previous tab selection self.showOtherTab = 0 } , content: { self.content}) .onAppear { DispatchQueue.main.async { self.isPresenting = true print("New listing sheet appeared with previous tab as tab \(self.appState.selectedTab).") } } } } struct ContentView: View { @EnvironmentObject var appState: AppState @State private var selection = 0 @State var newListingPresented = false var body: some View { // $appState.selectedTab back to just '$selection' TabView(selection: $appState.selectedTab){ // Browse Text("BrowseView") //BrowseView() .tabItem { Image(systemName: (selection == 0 ? "square.grid.2x2.fill" : "square.grid.2x2")).font(.system(size: 22)) } .tag(0) // New Listing SheetPresenter(isPresenting: $newListingPresented, showOtherTab: $appState.selectedTab, content: Text("1232"))//NewListingView(isPresented: self.$newListingPresented)) .tabItem { Image(systemName: "plus.square").font(.system(size: 22)) } .tag(1) // Bag // BagView() Text("BAGVIEW") .tabItem { Image(systemName: (selection == 2 ? "bag.fill" : "bag")).font(.system(size: 22)) } .tag(2) // Profile Text("ProfileView") // ProfileView() .tabItem { Image(systemName: (selection == 3 ? "person.crop.square.fill" : "person.crop.square")).font(.system(size: 22)) } .tag(3) }.edgesIgnoringSafeArea(.top) } }
© www.soinside.com 2019 - 2024. All rights reserved.