【问题标题】:Navigation Bar title font problem on ios 13ios 13上的导航栏标题字体问题
【发布时间】:2020-07-09 03:52:20
【问题描述】:

我使用的是 Xcode 11.4 和 iOS 13.4。 我已经使用 UINavigatinBar.appearance() 设置了导航栏标题自定义字体 它可以正常工作,但是在 iOS 13+ 上,当我尝试推送到另一个 VC 然后返回到父 VC 时,父 VC 标题字体突然被设置为默认字体,一秒钟后它又变回自定义字体。

下面是问题的 gif:

nav bar font problem

【问题讨论】:

    标签: ios swift xcode xcode11.4


    【解决方案1】:

    iOS 13.+ 有 UINavigationBarAppearance 方法来自定义 NavigationBar-Title 和 NavigationBar-BarButtonItems

    检查此代码,可能会对您有所帮助

        let titleFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.white ]
        let barButtonFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 14)! ]
    
        UINavigationBar.appearance().tintColor = UIColor.white // bar icons
    
        if #available(iOS 13.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.backgroundColor = .red // If you want different nav background color other than white
    
            appearance.titleTextAttributes = titleFontAttrs
            appearance.largeTitleTextAttributes = titleFontAttrs // If your app supports largeNavBarTitle
    
            UINavigationBar.appearance().isTranslucent = false
    
            appearance.buttonAppearance.normal.titleTextAttributes = barButtonFontAttrs
            appearance.buttonAppearance.highlighted.titleTextAttributes = barButtonFontAttrs
    
            UINavigationBar.appearance().standardAppearance = appearance
            UINavigationBar.appearance().compactAppearance = appearance
            UINavigationBar.appearance().scrollEdgeAppearance = appearance
        } else {
            UINavigationBar.appearance().barTintColor = .red // bar background
    
            UINavigationBar.appearance().titleTextAttributes = titleFontAttrs
    
            UINavigationBar.appearance().isTranslucent = false
    
            UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .normal)
            UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .highlighted)
        }
    

    【讨论】:

    • 奇怪,我用的是iOS 14 & Xcode 12,上面的代码根本不生效。
    • 这很奇怪,我在UINavigationController扩展中定义了上面的内容
    • @Noor 面临与 xCode12 和 iOS 14 相同的问题
    • @pawan_kumar 您的导航栏背景或栏按钮属性没有改变?还是两者兼而有之?
    • @Noor 我只检查导航标题。我使用了 UILabel。
    【解决方案2】:

    给你,在 viewDidAppear 中管理它:

    let lblTitle = UILabel()
    
    let titleAttribute: [NSAttributedString.Key: Any] = [.font: UIFont.boldSystemFont(ofSize: 21),
                                                    .foregroundColor: UIColor.black]
    
    let attributeString = NSMutableAttributedString(string: "Navigation Title", attributes: titleAttribute)
    
    lblTitle.attributedText = attributeString
    
    lblTitle.sizeToFit()
    navigationItem.titleView = lblTitle
    

    【讨论】:

    • 感谢它完美运行。顺便说一句,您知道为什么另一种方法突然不起作用吗?真的很奇怪。
    • 这是 Apple 的错误。确保在 mac 上添加反馈报告器,以便未来版本的 xcode 可以正确编译
    猜你喜欢
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2012-03-26
    相关资源
    最近更新 更多