【问题标题】:Font on Back Button on NavBar (Swift)NavBar 上的后退按钮上的字体(Swift)
【发布时间】:2020-02-04 18:38:15
【问题描述】:

我可以调整导航栏外观的所有其他方面 - 但“返回”的字体仍然很顽固。

下面的 MWE 显示了我尝试过但无济于事的四件事

1)

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 4)!], for: .normal)
    return true
}

2) 3) 4)

类 customNavigationController: UINavigationController {

override func viewDidLoad() {
    super.viewDidLoad()


    UIBarButtonItem.appearance().setTitleTextAttributes(
    [
        NSAttributedString.Key.font : UIFont(name: "Rockwell", size: 4)!,
        NSAttributedString.Key.foregroundColor : UIColor.white,
    ], for: .normal )

    navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Chalkduster", size: 7)!], for: .normal)

    navigationBar.topItem?.backBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 4)!], for: .normal)
}

}

【问题讨论】:

    标签: swift uinavigationcontroller uinavigationbar uinavigationitem uinavigationbarappearance


    【解决方案1】:

    在 iOS 13 中最简单:

    let app = UINavigationBarAppearance()
    app.backButtonAppearance.normal.titleTextAttributes = [
        // whatever
    ]
    UINavigationBar.appearance().standardAppearance = app
    

    在 iOS 13 之前,API 不会画出您想要画出的区别。您只需一次为所有后退按钮设置一个单独的栏按钮项标题文本属性。

    let title = // ...
    let back = UIBarButtonItem(title: title, style: .plain, target: nil, action: nil)
    back.setTitleTextAttributes([
        // whatever
    ], for: .normal)
    self.navigationItem.backBarButtonItem = back
    

    (还请记住,当 this 视图控制器可见时,您的后退栏按钮项目不是后退栏按钮项目,但是当 另一个 视图控制器被推到 顶部。)

    【讨论】:

    • 哦哦!漂亮!谢谢!
    • 低操作系统有没有对应的?
    • 很遗憾没有。
    • 这很难做到 - 不是吗?相比之下,导航栏上的标题栏字体微不足道。
    • 顺便说一句 - 只是说我的用例是 UINavigationBar 的子类 - 将您的代码放入 CustomNavigationBar 类可以很好地工作(对于 iOS13!)
    【解决方案2】:

    这个解决方案似乎对我很有效:

    // Set all fonts in the navigation controller
    class CustomNavigationController: UINavigationController {
        // Font names
        let normalFontName = "AppleSDGothicNeo-Medium"
        let boldFontName = "AppleSDGothicNeo-Bold"
    
        // Font size
        let fontSize = CGFloat(13)
    
        // Create fonts
        let backButtonFont = UIFont(name: normalFontName, size: fontSize)
        let titleFont = UIFont(name: boldFontName, size: fontSize)
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Set standard appearance
            let appearance = UINavigationBarAppearance()
            appearance.backButtonAppearance.normal.titleTextAttributes = [NSAttributedString.Key.font: backButtonFont]
            appearance.titleTextAttributes = [NSAttributedString.Key.font: titleFont]
            UINavigationBar.appearance().standardAppearance = appearance
        }
    }
    

    【讨论】:

    • 我认为这会起作用 - 但 UINavigationBarAppearance 仅适用于 ios13 及更高版本......仍然坚持复古风格......
    猜你喜欢
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 2016-11-13
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多