如何在swiftUI中更改TabView的背景颜色?

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

ios 开发新手。我没有选择 Objective-c/UIKit,而是选择 swift/swiftUI 来开始。

想更改 swiftUI 中 TabView 的背景,首先我尝试使用

background
修饰符但没用,然后我在开发人员文档中找不到任何解决此问题的方法。

最后我找到了一个解决方案here如下(使用UITabBar),它有效。

所以我对此很困惑:

  1. SwiftUI没有提供改变TabView背景颜色的方法?我们应该使用UIKit/UITabBar来做到这一点? SwiftUI 只是 UIKit 的包装?
  2. 如果问题 1 的答案是 YES,为什么 swiftUI 没有提供它?这是因为 swiftUI 的不完善或者只是 Apple 不希望 swiftUI 开发人员更改 TabBar 的背景颜色?在特定场景中我们确实要做一些事情但是 swiftUI 买不起,我应该额外使用 UIKit 吗?
  3. 我不是 iOS 开发者,只是对此感兴趣。我应该先学习 swift 中的 UIKit,还是先学习 Objective-c?从 swift/swiftUI 开始是一个错误吗?
import SwiftUI

struct HomeView: View {
    // Use UITabBar to do this.
    init() {
        UITabBar.appearance().backgroundColor = UIColor(.orange)
    }

    var body: some View {
        TabView {
            ClockScreen()
                .tabItem {
                    Image(systemName: "heart.fill")
                    Text("Favourites")
                }
            ScheduleScreen()
                .tabItem {
                    Image(systemName: "mappin.circle.fill")
                    Text("Friends")
                }
            ProfileScreen()
                .tabItem {
                    Image(systemName: "person.fill")
                    Text("Nearby")
                }
        }
    }
}

ios swift swiftui uikit
1个回答
0
投票

2024 年 4 月
要设置自定义背景颜色,这对我有用:

let myColor: Color = Color("my-color")

在具有 TabView 的视图中

 init() {
    let tabBarAppearance = UITabBarAppearance()
    tabBarAppearance.backgroundColor = UIColor(myColor)
    tabBarAppearance.configureWithTransparentBackground()
    UITabBar.appearance().isTranslucent = false
    UITabBar.appearance().backgroundColor = UIColor(myColor)
    UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
    UITabBar.appearance().standardAppearance = tabBarAppearance
}
© www.soinside.com 2019 - 2024. All rights reserved.