由于 .accentColor 已弃用,如何在 SwiftUI 中正确设置 TabBarIcon 颜色?

And*_*ord 5 uitabbaritem uitabbar ios swiftui

在 SwiftUI 中更改所选内容的颜色似乎相当困难UITabBarItem。为了在应用程序的中心位置定义设计,我尝试.appearance()这样做,但没有任何效果:

// Only effects the unselected items
UITabBar.appearance().unselectedItemTintColor = .red

// No effect, deprecated in iOS 8(!)
UITabBar.appearance().selectedImageTintColor = .cyan

// Only effects the title, not the icon 
UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.green], for: .normal)

// No effect
UITabBar.appearance().tintColor = .red
Run Code Online (Sandbox Code Playgroud)

View唯一有效的方法是不使用外观代理,而是使用SwiftUI 定义中的“手动”设置颜色.accentColor

struct MainView: View {
    var body: some View {
        TabView {
             SomeView()
                .tabItem {
                    Label("Main", systemImage: "list.dash")
                        .tint(.green) // No effect
                }
                .tint(.yellow) // No effect
        }
        .accentColor(.orange)
        
        // No effect
        //.tint(.red)
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,.accentColor也将被弃用:

'accentColor' 将在 iOS 的未来版本中被弃用:使用资产目录的强调色或 View.tint(_:) 代替

使用.tint在 上TabView没有效果。UITabBarItem没有tint外观属性。

如何在不使用的情况下正确设置图标颜色accentColor(最好通过外观)