swift 单击按钮后刷新导航栏背景色视图

koaltpgm  于 2023-01-19  发布在  Swift
关注(0)|答案(1)|浏览(107)

我不知道如何处理。我试过几次,都无法刷新视图。
我在我的应用程序中设计主题,允许用户单击按钮并更改整个应用程序中的颜色主题。除了我为设置导航栏背景颜色而创建的导航栏视图之外,这一切都按预期工作。

import SwiftUI

struct NavigationViewBackgroundColor: View {
    
    // THEME
    @ObservedObject var theme = ThemeSettings.shared
    var themes: [Theme] = themeData
    
    @Environment(\.refresh) private var refresh
    
  init() {
    let coloredAppearance = UINavigationBarAppearance()
    coloredAppearance.configureWithOpaqueBackground()
      coloredAppearance.backgroundColor = UIColor(Color(themes[self.theme.themeSettings].themePrimaryColor))//.darkGray
    coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor(Color(themes[self.theme.themeSettings].themeSecondaryColor))]
    coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor(Color(themes[self.theme.themeSettings].themeSecondaryColor))]
    
    UINavigationBar.appearance().standardAppearance = coloredAppearance
    UINavigationBar.appearance().compactAppearance = coloredAppearance
    UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
   
    
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().frame =  CGRect(x: 0, y: 0, width: 400, height: 4)
      
  } //:END OF INIT
     
  
  var body: some View {
   NavigationView {
    } //:END OF NAVIGATION VIEW
   .navigationBarTitle("",displayMode: .inline)
   .navigationBarHidden(true)
  } //:END OF BODY/VIEW
} //:END OF STRUT

struct NavigationViewBackgroundColor_Previews: PreviewProvider {
  static var previews: some View {
    NavigationViewBackgroundColor()
  }
}

在我实现颜色主题选项之前,这就像最初打算的那样工作。现在应用程序必须关闭并重新打开才能让导航栏颜色和/或这个视图刷新。
我不知道如何处理。我尝试创建一个@State变量更新程序:Bool = false和一个切换函数来查看它是否会强制和更新。没有运气。
当用户选择除此之外的其他视图时,所有其他视图均正确更新并更改颜色主题。
如有任何指导,不胜感激。
下面是我用来显示navigationViewBackgroundColor视图的视图,一旦用户选择了不同的颜色,我就刷新该视图。

import SwiftUI

struct AppView: View {
    
    // MARK: - PROPERTIES

// THEME
    @ObservedObject var theme = ThemeSettings.shared
    var themes: [Theme] = themeData
    
    
    // MARK: - BODY
    
    var body: some View {
        

                NavigationView {
                    VStack {
                        
                        ZStack {
                            
                          Text(“Navigation View”)
                            .foregroundcolor(Color(themes[self.theme.themeSettings].themePrimaryColor))
                            
                        } //: END OF ZSTACK
                        
                    } //: END OF BODY VSTACK
                    .edgesIgnoringSafeArea(.all)
                    .accentColor(Color(themes[self.theme.themeSettings].themeSecondaryColor))
                    .background(Color(themes[self.theme.themeSettings].themePrimaryColor))
                    .navigationBarTitle("",displayMode: .inline)
                    .navigationBarItems(
                    NavigationViewBackgroundColor()
                } //: END OF NAVIGATION VIEW
                .navigationViewStyle(StackNavigationViewStyle())
.onAppear {
                //:THIS IS ONLY FOR TESTING REMOVE BEFORE LAUNCH
                if UserDefaults.standard.bool(forKey: "Theme") {
                    UserDefaults.standard.removeObject(forKey: "Theme")
                    UserDefaults.standard.set(0, forKey: "Theme")
                } else {
                    UserDefaults.standard.set(0, forKey: "Theme")
                } //:END OF IF FOR THEME THIS WILL SET A DEFAULT
            } //:END OF ONAPPEAR

下面是存储配色方案的主题数据。

The model

mport SwiftUI

// MARK: - THEME MODEL

struct Theme: Identifiable {
  let id: Int
  let themeName: String
  let themePrimaryColor: String
  let themePrimaryLightColor: String
  let themeSecondaryColor: String
  let themeAccentColor: String 
}

The data

import SwiftUI

// MARK: - THEME DATA

let themeData: [Theme] = [
    Theme(id: 0, themeName: "Pink Theme", themePrimaryColor: "Theme-Pink-Primary-Color", themePrimaryLightColor: "Theme-Pink-Primary-Light-Color", themeSecondaryColor: "Theme-Pink-Secondary-Color", themeAccentColor: "Theme-Pink-Accent-Color"),
    Theme(id: 1, themeName: "Blue Theme", themePrimaryColor: "Theme-Blue-Primary-Color", themePrimaryLightColor: "Theme-Blue-Primary-Light-Color",themeSecondaryColor: "Theme-Blue-Secondary-Color", themeAccentColor: "Theme-Blue-Accent-Color"),
    Theme(id: 2, themeName: "Green Theme", themePrimaryColor: "Theme-Green-Primary-Color", themePrimaryLightColor: "Theme-Green-Primary-Light-Color",themeSecondaryColor: "Theme-Green-Secondary-Color", themeAccentColor: "Theme-Green-Accent-Color"),
    Theme(id: 3, themeName: "Orange Theme", themePrimaryColor: "Theme-Orange-Primary-Color", themePrimaryLightColor: "Theme-Orange-Primary-Light-Color",themeSecondaryColor: "Theme-Orange-Secondary-Color", themeAccentColor: "Theme-Orange-Accent-Color")
]

The observable object that will trap the color choice

import Foundation
import SwiftUI

// MARK: - THEME CLASS

final public class ThemeSettings: ObservableObject {
  @Published public var themeSettings: Int = UserDefaults.standard.integer(forKey: "Theme") {
    didSet {
      UserDefaults.standard.set(self.themeSettings, forKey: "Theme")
    } //:END OF SET
  } //:END OF PUBLISHED
  
  private init() {}
  public static let shared = ThemeSettings()
} //:END OF CLASS
6yt4nkrj

6yt4nkrj1#

我没有使用NavigationBackgroundView,而是在应用程序视图中添加了一个zStack,然后在忽略安全边缘的情况下设置了主题颜色,这样就设置了先前存在的导航栏的背景,并在用户每次更改颜色时更新。

ZStack {
  Color(themes[self.theme.themeSettings].themePrimaryColor)
                    .ignoresSafeArea()
  VStack{
    Text("Everything else went inside here")
  }
}

相关问题