为什么导航栏 - 外观 - Xcode 中的标准故事板设置优先于代码?

问题描述 投票:0回答:1

我有一个问题,我无法弄清楚为什么以下代码(由 ViewDidLoad() 在我的自定义 UINavigationController 类上调用)不起作用。

最后我发现是 Storyboard / Attributes Inspector / Appearances > Standard 复选框设置覆盖了导航栏。我有点困惑为什么在 viewDidLoad() 中调用的代码没有覆盖这个故事板设置?

Xcode 版本是 13.4.1.

private func fontOverrides() {
    
    navigationBar.titleTextAttributes = [
        .font: FontManager.navigationBarTitle.font, .foregroundColor: FontManager.navigationBar.color]
    
    let textAttributesNormal:[NSAttributedString.Key:Any] = [.font : FontManager.navigationBarButtons.font, .foregroundColor : FontManager.navigationBar.color ]
    let textAttributesHighlighted:[NSAttributedString.Key:Any] = [.font : FontManager.navigationBarButtons.font, .foregroundColor : FontManager.navigationBarButtons.colorSelected ]

    UIBarButtonItem.appearance().setTitleTextAttributes(textAttributesNormal, for: UIControl.State.normal)
    UIBarButtonItem.appearance().setTitleTextAttributes(textAttributesHighlighted, for: UIControl.State.highlighted) }
swift xcode uinavigationcontroller storyboard uinavigationbar
1个回答
0
投票

您在

viewDidLoad()
中调用的代码没有覆盖情节提要设置的原因可能是因为在调用
viewDidLoad()
方法后应用了情节提要设置。

从故事板加载视图控制器时,在调用视图控制器的

viewDidLoad()
方法之前,从故事板配置其视图和子视图的属性。这意味着您在
viewDidLoad()
中所做的任何更改都可能被故事板设置覆盖。

在您的情况下,属性检查器中导航栏的“标准”复选框设置会覆盖您的

fontOverrides()
方法中应用的字体设置。

为确保应用您的字体设置,您可以删除属性检查器中的“标准”复选框或修改属性检查器中的字体设置以匹配您要以编程方式应用的设置。

或者,您可以将字体覆盖代码移动到

viewWillAppear()
方法,该方法在应用故事板设置后调用,以确保您的设置优先。

© www.soinside.com 2019 - 2024. All rights reserved.