首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当NavigationBar是大标题时,barTintColor不适用

当NavigationBar是大标题时,barTintColor不适用
EN

Stack Overflow用户
提问于 2020-03-22 09:20:12
回答 3查看 1.7K关注 0票数 2

我正在更新一个在Xcode10上编译的应用程序,在iOS 13上运行得很好。我想做一些更改,所以在Xcode11上重新编译,现在barTintColor有问题。

如果'Large Titles‘设置为'Always’,我的自定义barTintColor就不会被应用--我只会得到默认的灰色。如果'Large Titles‘设置为'Never',我的自定义barTintColor将按预期应用。如果“大标题”设置为“自动”,则当显示大标题时,NavBar将默认为灰色,而当显示小标题时,将显示我的自定义颜色。例如,当我的导航栏下面的标题被向上推时,默认的大标题切换为小标题,并且我的NavBar会改变颜色。正常的行为是它总是我的自定义颜色。

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

代码语言:javascript
复制
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)

}

你知道为什么行为改变了吗,以及如何修复它?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-03-22 10:05:11

Stack Overflow用户

发布于 2020-03-23 04:58:06

glotcha指出,苹果的文档对于解决这个问题至关重要,尽管还有更多的东西。以下是适用于iOS 13的my setNavigationBarItems()的更新版本:

代码语言:javascript
复制
    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
    }

在我的例子中,一个关键点是我的导航栏(在Autolayout中)设置为“Large Titles”为“Automatic”。这使得有必要包含.scrollEdgeAppearance行,以便在从大到紧凑转换时应用自定义外观。事实证明,.compactAppearance行并不是必需的,因为我对两者使用相同的颜色。如果我想为大型和紧凑的应用程序设置不同的外观,那么包含.compactAppearance的行也会很有用。

票数 5
EN

Stack Overflow用户

发布于 2021-07-17 23:05:20

如果你想在一个单独的地方完成整个应用:

代码语言:javascript
复制
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
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60795035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档