【问题标题】:barTintColor not applied when NavigationBar is Large Titles当 NavigationBar 为大标题时不应用 barTintColor
【发布时间】:2020-07-02 19:44:46
【问题描述】:

我正在更新一个在 Xcode 10 上编译并在 iOS 13 上运行良好的应用程序。我想做一些更改,因此在 Xcode 11 上重新编译,现在 barTintColor 出现问题。

如果“Large Titles”设置为“Always”,我的自定义 barTintColor 不会被应用——我只会得到默认的灰色。如果“大标题”设置为“从不”,我的自定义 barTintColor 将按预期应用。如果“大标题”设置为“自动”,则显示大标题时导航栏默认为灰色,显示小标题时默认为我的自定义颜色。比如我的nav bar下面的TableView被推上去的时候,默认的大标题切换到小标题,我的NavBar变色。正常行为是它始终是我的自定义颜色。

我的 ViewController 类中的相关代码,最后一行是设置 barTintColor 的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    setDelegates()
    setTableViewHeightForCollapsingHeaders()
    setNavigtionBarItems()
    doSplitViewManagement()
}


override func viewWillAppear(_ animated: Bool) {
    clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed
    super.viewWillAppear(animated)
    updateUI()
}

fileprivate func setNavigtionBarItems() {
    //set up UI buttons
    navigationItem.leftBarButtonItem = editButtonItem
    let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:)))
    navigationItem.rightBarButtonItem = addButton

    navigationController?.navigationBar.barTintColor = UIColor(hex: 0x5da0a2)

}

知道行为发生变化的原因以及如何解决吗?

【问题讨论】:

    标签: ios swift uinavigationbar uinavigationbarappearance


    【解决方案1】:

    【讨论】:

    • 这就是我要找的——谢谢!在尝试使用上述文档实现之后,我必须解决一些细微的问题,所以我也会自己回答这个问题。希望这在承认您的贡献方面是适当的。
    【解决方案2】:

    glotcha 指出的 Apple 文档对于解决问题至关重要,尽管还有更多内容。这是适用于 iOS 13 的 setNavigationBarItems() 的更新版本:

    fileprivate func setNavigtionBarItems() {
    
        if #available(iOS 13.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithDefaultBackground()
            appearance.backgroundColor = myBackgroundColor
    
            navigationController?.navigationBar.standardAppearance = appearance
            navigationController?.navigationBar.scrollEdgeAppearance = appearance
            //navigationController?.navigationBar.compactAppearance = appearance
            
        } else {
            // Fallback on earlier versions
            navigationController?.navigationBar.barTintColor = myBackgroundColor
        }
    }
    

    在我的案例中,一个关键点是我的导航栏(在自动布局中)设置为“大标题”为“自动”。这使得有必要包含.scrollEdgeAppearance 行,以便在它从大到紧凑转换时应用自定义外观。原来.compactAppearance 行不是必需的,因为我对两者都使用了相同的颜色。如果我想要大而紧凑的不同外观设置,那么.compactAppearance 的行也很有用。

    【讨论】:

    • 很好的答案。很难找到正确的解决方案。我有同样的问题,但现在,你的解决方案解决了。干得好!
    • 谢谢,我用了大标题,看来这是唯一正确的解决方案。 BTW,你知道如何更改largeTitle的字体和大小吗?
    【解决方案3】:

    如果您想在一个地方为整个应用执行此操作:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        let barBackgroundColor = UIColor(displayP3Red: 47/255, green: 54/255, blue: 64/255, alpha: 1.0)
        
        let appearance = UINavigationBarAppearance()
        appearance.configureWithDefaultBackground()
        appearance.backgroundColor = barBackgroundColor
        appearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
        appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    
        UINavigationBar.appearance().standardAppearance = appearance
        UINavigationBar.appearance().scrollEdgeAppearance = appearance
    
        return true
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      • 2014-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多