繁体   English   中英

ios 13上的导航栏标题字体问题

[英]Navigation Bar title font problem on ios 13

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

下面是问题的gif:

导航栏字体问题

给你,在 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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM