iOS 13上的导航栏标题字体问题 [英] Navigation Bar title font problem on ios 13

查看:445
本文介绍了iOS 13上的导航栏标题字体问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

以下是问题的gif图像:

导航栏字体问题

解决方案

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)
    }

I’m using Xcode 11.4 and iOS 13.4. I have set navigation bar title custom font using UINavigatinBar.appearance() And it works correctly but on iOS 13+ when i try to push to another VC and then comeback to the parent VC, the parent VC title font suddenly has been set to default font and after a second it changes back to the custom font.

Below is a gif of the problem:

nav bar font problem

解决方案

iOS 13.+ has UINavigationBarAppearance approach to customize NavigationBar-Title & NavigationBar-BarButtonItems

Check this code, might help you

    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 13上的导航栏标题字体问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆