SwiftUI中的NavigationBarTitle文本颜色

3

我想要改变我的navigationBarTitle的颜色(不是背景)。我尝试了几种方法,但是当我打开屏幕时,标题文字颜色只有在我向下滚动时才会改变。

输入图像描述

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()
        }
    }
3个回答

5
您需要为大标题添加相同的内容。
.background (NavigationConfigurator { nc in
    
    nc.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.blue]
    nc.navigationBar.largeTitleTextAttributes = [.foregroundColor : UIColor.blue]
    
})

另一种选择是使用外观(appearance)代替配置器(configurator),但应该在视图的 init 中完成(在创建 NavigationView 之前),例如:

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

谢谢,它起作用了。我不知道largetitle代表不同的文本。 - Begbie

1
你还可以采用更完整的方法:

    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())
    }

结果如下:

enter image description here


1

在初始化时调用此函数

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
  }

感谢您为Stack Overflow社区做出的贡献。这可能是一个正确的答案,但如果您能提供代码的额外解释,那将非常有用,这样开发人员就可以理解您的推理过程。对于不太熟悉语法或难以理解概念的新开发人员来说,这尤其有用。为了造福社区,您能否编辑您的答案并包含更多细节呢? - Jeremy Caney

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接