【问题标题】:NavigationBarTitle Text Color in SwiftUISwiftUI 中的 NavigationBarTitle 文本颜色
【发布时间】:2020-12-15 07:30:58
【问题描述】:

我正在尝试更改我的 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()
        }
    }

【问题讨论】:

    标签: swift swiftui navigationbar


    【解决方案1】:

    你需要为大标题添加相同的内容

    .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]
    }
    

    【讨论】:

    • 谢谢你成功了。我不知道 largetitle 代表不同的文本。
    【解决方案2】:

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

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

    结果如下:

    【讨论】:

      猜你喜欢
      • 2020-01-21
      • 2020-04-16
      • 2021-01-06
      • 2020-01-09
      • 2021-09-18
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多