I have a tvOS App with a TabView across the top. I want to use the background color of the TabView to indicate status. Initially it will be red and when something occurs in one of the views, I want to change the background color of the TabView to green.
I'm using the UITabBar.appearance().barTintColor = UIColor.red
in my init()
to set the initial color to red, but I can't find a way to change that to green later in execution.
struct ContentView: View {
@State private var selection = 1
init() {
UITabBar.appearance().barTintColor = UIColor.red
}
var body: some View {
TabView (selection:$selection){
Tab1View()
.tabItem {
Image(systemName: "1.square.fill")
Text("Tab 1")
}
.tag(1)
Tab2View()
.tabItem {
Image(systemName: "2.square.fill")
Text("Tab 2")
}.tag(2)
Tab3View()
.tabItem {
Image(systemName: "3.square.fill")
Text("Tab 3")
}.tag(3)
}
.font(.headline)
.accentColor(.white)
.ignoresSafeArea()
}
}
You can use the code below.
import SwiftUI
struct ContentView: View {
@State private var selectedTab = 0
var tabViewTitle = ["First Page","Second Page","Third Page"]
var body: some View {
VStack {
TabView(selection: $selectedTab){
Text(tabViewTitle[0])
.tag(0)
Text(tabViewTitle[1])
.tag(1)
Text(tabViewTitle[2])
.tag(2)
}
.frame(width: 300, height: 500)
.tabViewStyle(PageTabViewStyle())
.background(Color.purple)
.cornerRadius(15)
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
HStack(spacing: 16) {
ForEach(tabViewTitle, id: \.self) { index in
Rectangle()
.fill(self.selectedTab == tabViewTitle.firstIndex(where: {$0 == index}) ? Color.purple : Color.purple.opacity(0.5))
.frame(width: self.selectedTab == tabViewTitle.firstIndex(where: {$0 == index}) ? 16 : 12,height: self.selectedTab == tabViewTitle.firstIndex(where: {$0 == index}) ? 16 : 12)
}
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.