SwiftUI的TabView颜色无法更改为自定义颜色

时间:2020-09-05 00:58:23

标签: ios swift swiftui

这是我的TabView

struct HomeView: View {
    let auth: UserAuth
    init(auth: UserAuth) {
        self.auth = auth
        UITabBar.appearance().barTintColor = UIColor(named: "CustomOrange")
        UITabBar.appearance().unselectedItemTintColor = UIColor.white.withAlphaComponent(0.6)
        UITabBar.appearance().autoresizesSubviews = true
        UITabBar.appearance().backgroundColor = UIColor(named: "CustomOrange")
    }
    
    var body: some View {
        ZStack {
                TabView(selection: $selectedTab) {
                    Settings(auth: auth)
                    .tabItem {
                        Image(systemName: "gear")
                        Text("Settings")
                    }.tag(0)

当我将Tabview栏的颜色设置为自定义橙色时:

UITabBar.appearance().barTintColor = UIColor(named: "CustomOrange")

它始终默认为系统橙色(相当于Color.orange

我的自定义橙色是#ff8000。

标签栏颜色是否限于系统颜色?

1 个答案:

答案 0 :(得分:1)

在Xcode 12 / iOS 14上运行良好。它是淡色(非纯色)(如果使用选择器测试结果)。要获得纯分配的颜色,您必须禁用半透明性

    UITabBar.appearance().isTranslucent = false