ios 开发新手。我没有选择 Objective-c/UIKit,而是选择 swift/swiftUI 来开始。
想更改 swiftUI 中 TabView 的背景,首先我尝试使用
background
修饰符但没用,然后我在开发人员文档中找不到任何解决此问题的方法。
最后我找到了一个解决方案here如下(使用UITabBar),它有效。
所以我对此很困惑:
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")
}
}
}
}
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
}