どうも、ちょげ(@chogetarou)です。
TabViewのタブバーの色を変える方法を紹介します。
方法
TabViewのタブバーの色を変えるには、UITabBarを使います。
具体的には、initメソッドやonAppear修飾子などで、UITabBar.appearance().backgroundColorにタブバーの色を代入します。
init {
//タブバーの色を代入
UITabBar.appearance().backgroundColor = barColor
}
UITabBar.appearance().backgroundColorに代入した色が、TabViewのタブバーの色になります。
使用例
struct ContentView: View {
init() {
UITabBar.appearance().backgroundColor = .green.withAlphaComponent(0.2)
}
var body: some View {
VStack {
TabView {
PageView(text: "Home View")
.tabItem {
Image(systemName: "house.fill")
Text("home")
}
PageView(text: "Data View")
.tabItem {
Image(systemName: "folder.fill")
Text("Data")
}
PageView(text: "Person View")
.tabItem {
Image(systemName: "person.fill")
Text("Person")
}
}
}
}
}
コメント