SwiftUI中的NavigationBarTitle文本颜色

41zrol4v  于 2023-05-28  发布在  Swift
关注(0)|答案(3)|浏览(222)

我正在尝试更改我的导航栏标题颜色(而不是背景)。我尝试了一对夫妇的解决方案,但我的标题文本颜色只改变当我向下滚动。当屏幕打开时不改变。

var body: some View {
  ZStack{
    NavigationView{
      ScrollView{
        LazyVStack{
          ForEach(storeArray,id:\.id) { item in
            Image(uiImage: (item.banner?.url)!.load())
                .resizable()
                .frame(width: CGFloat(UIScreen.main.bounds.width * 0.9), height: CGFloat((UIScreen.main.bounds.width / CGFloat((item.banner?.ratio) ?? 1))))
                .cornerRadius(12)
                .shadow(radius: 4)
                .aspectRatio(CGFloat((item.banner?.ratio)!), contentMode: .fit)
          }
        }
      }
      .navigationBarTitle(Text("United App").foregroundColor(.blue))
      .background (NavigationConfigurator { nc in
          nc.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.blue]
      })
    }
    .onAppear {
        if isOpened != true {
            getStoreResponse()
        }
    }
zbdgwd5y

zbdgwd5y1#

您需要添加相同的大标题

.background (NavigationConfigurator { nc in
    
    nc.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.blue]
    nc.navigationBar.largeTitleTextAttributes = [.foregroundColor : UIColor.blue]
    
})

另一种方法是使用外观而不是配置器,但应该在视图init中(在创建NavigationView之前)完成,如

init() {
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.blue]
    UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.blue]
}
b5buobof

b5buobof2#

您还可以使用更完整的方法:

init() {
        let coloredAppearance = UINavigationBarAppearance()

            // for inline displayMode
            coloredAppearance.titleTextAttributes = [.font : UIFont(name: "Georgia", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.blue]
            
            // for automatic and large displayMode
            coloredAppearance.largeTitleTextAttributes = [.font : UIFont(name: "Georgia", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.black]
            
            // or
            
            // for inline displayMode
    //        coloredAppearance.titleTextAttributes = [.font:UIFont.preferredFont(forTextStyle: .title1), NSAttributedString.Key.foregroundColor: UIColor.blue]
            // for automatic and large displayMode
    //        coloredAppearance.largeTitleTextAttributes = [.font:UIFont.preferredFont(forTextStyle: .title1), NSAttributedString.Key.foregroundColor: UIColor.black]

            // to make everything work normally
            UINavigationBar.appearance().standardAppearance = coloredAppearance
            UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
    }
    var body: some View {
        NavigationView {
            Text("Screnn")
                .navigationBarTitle("Title", displayMode: .inline)
        }
        // Do not forget this
        // that means only show one view at a time no matter what device I'm working
        .navigationViewStyle(StackNavigationViewStyle())
    }

结果如下:

brtdzjyr

brtdzjyr3#

在initialize中调用这个

init() {
    setNavigationAppearance()
   }

使用此函数可更改swiftUI中的导航栏标题和背景颜色

func setNavigationAppearance() {
    let appearance = UINavigationBarAppearance()
    appearance.configureWithOpaqueBackground()
    appearance.backgroundColor = .systemBackground
    
    let attrs: [NSAttributedString.Key: Any] = [
      .foregroundColor: UIColor.systemBackground,
      .font: UIFont.monospacedSystemFont(ofSize: 56, weight: .black)
    ]
    appearance.largeTitleTextAttributes = attrs
    UINavigationBar.appearance().scrollEdgeAppearance = appearance
  }

相关问题