Xcode 11.4 导航栏按钮问题

Posted

技术标签:

【中文标题】Xcode 11.4 导航栏按钮问题【英文标题】:Xcode 11.4 Navigation Bar Button Issues 【发布时间】:2020-03-27 16:49:38 【问题描述】:

自从更新到 Xcode 11.4 后,我遇到了一个问题,即我无法弄清楚如何更改导航栏中 后退箭头 的颜色。

在我能够简单地使用之前:

navigationBar.barTintColor = DisplayUtils.sweetGreenColor()

但是,11.4 迫使我使用 standardAppearance 功能来设置导航栏和所有内容的颜色。这没什么大不了的,这适用于设置 barTint 和标题颜色:

navigationBar.standardAppearance.backgroundColor = DisplayUtils.sweetGreenColor()

let buttonAppearance = UIBarButtonItemAppearance()
buttonAppearance.normal.titleTextAttributes = [.foregroundColor : DisplayUtils.whiteColor()]
navigationBar.standardAppearance.buttonAppearance = buttonAppearance

navigationBar.standardAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: DisplayUtils.whiteColor(), NSAttributedString.Key.font: UIFont(name: "Roboto-Bold", size: 18) as Any]

我只是没有在 UIBarButtonItemAppearance 对象上看到替换 barTintColor 的属性。它可以让你改变文本的颜色,改变图像本身,但没有颜色属性。

【问题讨论】:

【参考方案1】:

我找到了适用于我的实例的解决方案。它之所以有效,是因为我正在使用以下代码创建视图控制器:

if let stbrd = self.storyboard 
   if let vc: ViewController = stbrd.instantiateViewController(withIdentifier: "VC") as? ViewController 
                self.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil) // remove back button text
                self.navigationItem.backBarButtonItem?.tintColor = DisplayUtils.sweetGreenColor()
                self.navigationController?.pushViewController(vc, animated: true)
   

就是这样:

self.navigationItem.backBarButtonItem?.tintColor = DisplayUtils.sweetGreenColor()

这会正确设置新呈现的视图控制器上的后退箭头的颜色。

我仍然不知道如何像在此更改之前那样简单地在视图控制器本身上设置该颜色。所以,如果有人能提供这些知识,那就太好了。

【讨论】:

【参考方案2】:

在情节提要上,为您的导航控制器将“条形色调”更改为其“默认”值,然后在您的代码上像往常一样更改它。

通过以编程方式简单地首先将导航栏颜色更改为默认值,然后您可以更新您已经在做的事情。

    if #available(ios 13.0, *) 
        self.navigationController?.navigationBar.standardAppearance.configureWithDefaultBackground()
    

【讨论】:

以上是关于Xcode 11.4 导航栏按钮问题的主要内容,如果未能解决你的问题,请参考以下文章

Xcode 导航栏按钮对齐

不会将导航项添加到 xcode 中的导航栏

xcode 5中的导航栏对象不显示后退按钮并且没有功能

XCTest Objective C Xcode8.1如何提取任何按钮或导航栏的标签/值/名称

隐藏/显示导航栏中两个右栏按钮之一

如何使用左侧的后退按钮创建导航栏?