在TabView SwiftUI中禁用项目

在TabView SwiftUI中禁用项目,swiftui,tabview,Swiftui,Tabview,如何将项目设置为禁用(不可单击),但在选项卡视图中可见 TabView(selection: $selectedTab) { Settings() .tabItem { Image(systemName: "gearshape.fill") Text("Settings") }.tag(1)

如何将项目设置为禁用(不可单击),但在选项卡视图中可见

TabView(selection: $selectedTab) {
            Settings()
                .tabItem {
                    Image(systemName: "gearshape.fill")
                    Text("Settings")
                }.tag(1)
                 .disabled(true) // Not Working
          

目前还没有直接的SwiftUI工具(SwiftUI 2.0),因此请根据我的另一个答案中的
TabBarAccessor
查找以下可能的方法

使用Xcode 12.1/iOS 14.1进行测试(注意-仅为演示而更改色调,因为禁用的项目为灰色,在灰色选项卡上不可见)

struct TestTabBar:视图{
init(){
uitabar.appearance().unselectedItemTintColor=UIColor.green
}
@国家私有变量选择=0
var body:一些观点{
选项卡视图(选择:$selection){
文本(“第一视图”)
.background(TabBarAccessor{tabBar in

tabBar.items?.last?.isEnabled=false/我只是创建了一种方法来实现您想要的完全支持和可定制的功能

使用Xcode 12.1版、iOS 14.1进行测试,结果如下:

    import SwiftUI

struct ContentView: View {
    

    @State private var selection = 0
    @State private var exSelection = 0
    private var disableThis = 2
    
    
    
    var body: some View
    {
        TabView(selection: $selection)
        {
            viewFinder(selectedIndex: selection == disableThis ? $exSelection : $selection)
                .tabItem { Image(systemName: "1.circle") }
                .tag(0)
            
            
            viewFinder(selectedIndex: selection == disableThis ? $exSelection : $selection)
                .tabItem { Image(systemName: "2.circle") }
                .tag(1)
            
            viewFinder(selectedIndex: selection == disableThis ? $exSelection : $selection)
                .tabItem { Image(systemName: "3.circle") }
                .tag(2)
            
            viewFinder(selectedIndex: selection == disableThis ? $exSelection : $selection)
                .tabItem { Image(systemName: "4.circle") }
                .tag(3)
        }
        .onAppear()
        {
            UITabBar.appearance().barTintColor = .white
        }
        .accentColor(selection == disableThis ? Color.gray : Color.red)
        .onChange(of: selection) { _ in
            if selection != disableThis { exSelection = selection } else { selection = exSelection }
        }
        
        
    }
    
    
    
}





struct viewFinder: View
{
    
    @Binding var selectedIndex: Int
    
    var body: some View {
        
        
        return Group
        {
            if      selectedIndex == 0
            {
                FirstView()
            }
            else if selectedIndex == 1
            {
                SecondView()
            }
            else if selectedIndex == 2
            {
                ThirdView()
            }
            else if selectedIndex == 3
            {
                FourthView()
            }
            else
            {
                EmptyView()
            }
        }
        

        
    }
    
}


struct FirstView: View { var body: some View {Text("FirstView")}}
struct SecondView: View { var body: some View {Text("SecondView")}}
struct ThirdView: View { var body: some View {Text("ThirdView")}}
struct FourthView: View { var body: some View {Text("FourthView")}}

我有一个错误:“从初始值设定项返回而不初始化所有存储的属性”试图解决它,如果我复制粘贴您的代码,我有一条消息:“在范围中找不到'TabBarAccessor'”我给出了链接-请从头到尾仔细阅读。错误:线程1:EXC_BAD_ACCESS(代码=1,地址=0xA3AB)不明白它的意思…版本12.1(12A7403)
    import SwiftUI

struct ContentView: View {
    

    @State private var selection = 0
    @State private var exSelection = 0
    private var disableThis = 2
    
    
    
    var body: some View
    {
        TabView(selection: $selection)
        {
            viewFinder(selectedIndex: selection == disableThis ? $exSelection : $selection)
                .tabItem { Image(systemName: "1.circle") }
                .tag(0)
            
            
            viewFinder(selectedIndex: selection == disableThis ? $exSelection : $selection)
                .tabItem { Image(systemName: "2.circle") }
                .tag(1)
            
            viewFinder(selectedIndex: selection == disableThis ? $exSelection : $selection)
                .tabItem { Image(systemName: "3.circle") }
                .tag(2)
            
            viewFinder(selectedIndex: selection == disableThis ? $exSelection : $selection)
                .tabItem { Image(systemName: "4.circle") }
                .tag(3)
        }
        .onAppear()
        {
            UITabBar.appearance().barTintColor = .white
        }
        .accentColor(selection == disableThis ? Color.gray : Color.red)
        .onChange(of: selection) { _ in
            if selection != disableThis { exSelection = selection } else { selection = exSelection }
        }
        
        
    }
    
    
    
}





struct viewFinder: View
{
    
    @Binding var selectedIndex: Int
    
    var body: some View {
        
        
        return Group
        {
            if      selectedIndex == 0
            {
                FirstView()
            }
            else if selectedIndex == 1
            {
                SecondView()
            }
            else if selectedIndex == 2
            {
                ThirdView()
            }
            else if selectedIndex == 3
            {
                FourthView()
            }
            else
            {
                EmptyView()
            }
        }
        

        
    }
    
}


struct FirstView: View { var body: some View {Text("FirstView")}}
struct SecondView: View { var body: some View {Text("SecondView")}}
struct ThirdView: View { var body: some View {Text("ThirdView")}}
struct FourthView: View { var body: some View {Text("FourthView")}}