首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ios 13上的导航栏标题字体问题

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

Stack Overflow用户
提问于 2020-03-28 23:06:45
回答 2查看 1.5K关注 0票数 1

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

下面是问题的gif:

nav bar font problem

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-05-10 21:06:50

iOS 13.+使用UINavigationBarAppearance方法来自定义导航栏-标题和导航栏-栏按钮项

检查这段代码,可能会对你有所帮助

代码语言:javascript
复制
    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)
    }
票数 4
EN

Stack Overflow用户

发布于 2020-03-29 00:07:38

在这里,您可以在viewDidAppear中管理它:

代码语言:javascript
复制
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
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60902418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档