【问题标题】:Disable item in TabView SwiftUI在 TabView SwiftUI 中禁用项目
【发布时间】:2021-02-19 22:13:16
【问题描述】:

如何将项目设置为禁用(不可点击)但在我的 tabView 中可见?

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

【问题讨论】:

    标签: swiftui tabview


    【解决方案1】:

    现在没有直接的 SwiftUI 工具(SwiftUI 2.0),所以从我的另一个答案 https://stackoverflow.com/a/59972635/12299030 中找到基于 TabBarAccessor 的以下可能方法。

    使用 Xcode 12.1 / iOS 14.1 测试(注意 - 仅针对演示更改了色调颜色,因为禁用的项目是灰色且在灰色标签栏上不可见)

    struct TestTabBar: View {
        init() {
            UITabBar.appearance().unselectedItemTintColor = UIColor.green
        }
        @State private var selection = 0
        var body: some View {
            TabView(selection: $selection) {
                Text("First View")
                    .background(TabBarAccessor { tabBar in
                        tabBar.items?.last?.isEnabled = false     // << here !!
                    })
                    .tabItem { Image(systemName: "1.circle") }
                    .tag(0)
                Text("Second View")
                    .tabItem { Image(systemName: "2.circle") }
                    .tag(1)
            }
        }
    }
    

    【讨论】:

    • 我有这个错误:“从初始化程序返回而不初始化所有存储的属性”试图解决它
    • 如果我复制粘贴您的代码,我会收到以下消息:“在范围内找不到 'TabBarAccessor'”
    • 我为此提供了链接 - 从头开始​​仔细阅读。
    【解决方案2】:

    我只是创建了一种方法来做你想要的完全支持和可定制的事情!

    使用 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")}}
    

    【讨论】:

    • 错误:线程 1:EXC_BAD_ACCESS (code=1, address=0xa3a3a3a3a3a3a3ab) 不明白是什么意思...
    • 版本 12.1 (12A7403)