Swiftui 快捷界面:更新导航栏颜色
目标:按按钮更新导航栏颜色 当前:导航栏颜色完全不变 尝试:我目前正在使用init()设置NavBarIntColor&我也尝试过,但都不会根据需要进行更新 代码:Swiftui 快捷界面:更新导航栏颜色,swiftui,Swiftui,目标:按按钮更新导航栏颜色 当前:导航栏颜色完全不变 尝试:我目前正在使用init()设置NavBarIntColor&我也尝试过,但都不会根据需要进行更新 代码: var navColor = Color.green struct ContentView: View { @State var themeColor = Color.green @State var items = [0,1,2,3,4,5,6,7,8,9] @State var showSheet =
var navColor = Color.green
struct ContentView: View {
@State var themeColor = Color.green
@State var items = [0,1,2,3,4,5,6,7,8,9]
@State var showSheet = false
init() {
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.black]
UINavigationBar.appearance().barTintColor = UIColor(navColor)
UINavigationBar.appearance().backgroundColor = UIColor(navColor)
}
var body: some View {
NavigationView{
List{
ForEach(0..<items.count){ i in
Text("Item: \(items[i])")
}.listRowBackground(themeColor)
}
.navigationBarTitle("Theme", displayMode: .inline)
.navigationBarItems(leading: Button(action: {
showSheet.toggle()
}, label: {
Text("Sheet")
})
,trailing: Button(action: {
self.themeColor = Color(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1))
navColor = Color(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1))
items.shuffle()
}, label: {
Text("Random Color")
}))
// .background(NavigationConfigurator { nc in
// nc.navigationBar.barTintColor = UIColor(navColor)
// nc.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.black]
// })
.sheet(isPresented: $showSheet, content: {
popSheet()
})
}
}
}
struct popSheet: View {
var body: some View{
NavigationView{
Text("Hello")
.navigationBarTitle("Sheet", displayMode: .inline)
}
}
}
var navColor=Color.green
结构ContentView:View{
@状态变量themeColor=Color.green
@状态变量项=[0,1,2,3,4,5,6,7,8,9]
@状态变量showSheet=false
init(){
UINavigationBar.appearance().titleTextAttributes=[.foregroundColor:UIColor.black]
UINavigationBar.appearance().barTintColor=UIColor(navColor)
UINavigationBar.appearance().backgroundColor=UIColor(navColor)
}
var body:一些观点{
导航视图{
名单{
ForEach(0..Void={uin}
func makeUIViewController(上下文:UIViewControllerRepresentableContext)->UIViewController{
UIViewController()
}
func updateUIViewController(uViewController:uiViewController,上下文:UIViewControllerRepresentableContext){
如果让nc=uiViewController.navigationController{
自配置(nc)
}
}
}
您可以使用.id(主题颜色)
强制刷新导航视图
以下是一个可能的解决方案:
struct ContentView: View {
@State var themeColor = Color.green
init() {
updateNavigationBarColor()
}
var body: some View {
NavigationView {
VStack {
Button("Change to blue") {
themeColor = .blue
updateNavigationBarColor()
}
Button("Change to red") {
themeColor = .red
updateNavigationBarColor()
}
}
.navigationTitle("Title")
}
.id(themeColor)
}
func updateNavigationBarColor() {
UINavigationBar.appearance().barTintColor = UIColor(themeColor)
UINavigationBar.appearance().backgroundColor = UIColor(themeColor)
}
}
struct ContentView: View {
@State var themeColor = Color.green
init() {
updateNavigationBarColor()
}
var body: some View {
NavigationView {
VStack {
Button("Change to blue") {
themeColor = .blue
updateNavigationBarColor()
}
Button("Change to red") {
themeColor = .red
updateNavigationBarColor()
}
}
.navigationTitle("Title")
}
.id(themeColor)
}
func updateNavigationBarColor() {
UINavigationBar.appearance().barTintColor = UIColor(themeColor)
UINavigationBar.appearance().backgroundColor = UIColor(themeColor)
}
}