在swift 4下更改导航控制器后退按钮的字体

Posted

技术标签:

【中文标题】在swift 4下更改导航控制器后退按钮的字体【英文标题】:Change the font on the back button of a navigation controller under swift 4 【发布时间】:2018-02-14 16:07:12 【问题描述】:

Swift 4、ios 11.2.5 Xcode 9.2

尝试更改后退按钮的字体。尝试找到以前的解决方案,但似乎没有一个在 Swift 4、iOS 11.2.5 下使用我的配置、标签栏控制器中的导航控制器。

得到这个代码,第一行和最后一行都可以,但是中间三行不行。

self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!]
navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .normal)
navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .highlighted)
navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .focused)
navigationItem.title = "Filter \(titleSelection!) [shake to clear]"

这是在viewDidLoad 方法中。这应该有效吗?

【问题讨论】:

我更新了我的答案。 change font of back button on uinavigationcontroller的可能重复 【参考方案1】:

对于 Swift 4,你可以在 AppDelegate 中试试这个。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 

        UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red, NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 15)!], for: UIControlState.normal)

        UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.green, NSAttributedStringKey.font : UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)! ], for: .highlighted)
        UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.blue, NSAttributedStringKey.font : UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)! ], for: .focused)

        return true

ViewController中。

override func viewWillAppear(_ animated: Bool) 

    self.title = "List"
    self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!]

    // THE BELOW THREE LINES NOT WORKING.

    //navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .normal)

    //navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .highlighted)

    //navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .focused)


故事板

输出

【讨论】:

对这个也寄予厚望;但不幸的是也没有为我工作。我猜这里有一些奇怪的设置。 是的,这行得通!将代码放入应用程序委托的秘诀似乎!谢谢!! 通过添加到 AppDelegate,这对我来说非常适合整个应用程序。【参考方案2】:

您可以通过执行以下操作来更改应用程序委托中的字体,这将在整个应用程序中而不是在一个视图控制器中更改字体。

if let customFont = UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20.0) 
    UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont], for: .normal)

【讨论】:

【参考方案3】:

代替:

navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .normal)

试试:

self.navigationController?.navigationBar.topItem?.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .normal)

【讨论】:

【参考方案4】:

Swift 5 中,您可以通过以下方式做到这一点:

    let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: .regular)]
    self.navigationItem.backBarButtonItem?.setTitleTextAttributes(attributes, for: .normal)

请注意,它对下一个推送的视图控制器而不是当前显示的视图控制器有效,这就是为什么它非常混乱!

另外,检查情节提要并选择 previous 视图控制器的导航项,然后在 Back Button(Inspector)中输入一些内容。

【讨论】:

【参考方案5】:

要使其在 iOS 13.0 上运行,您应该使用 UINavigationBarAppearience。为了更改导航栏中所有元素的字体,您可以执行以下操作:

if #available(iOS 13.0, *) 
    let navBarAppearance = UINavigationBarAppearance()
    let attributes = [NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!]
    navBarAppearance.titleTextAttributes = attributes
    navBarAppearance.buttonAppearance.normal.titleTextAttributes = attributes
    navBarAppearance.doneButtonAppearance.normal.titleTextAttributes = attributes

    self.navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
    self.navigationController?.navigationBar.standardAppearance = navBarAppearance

【讨论】:

以上是关于在swift 4下更改导航控制器后退按钮的字体的主要内容,如果未能解决你的问题,请参考以下文章

如何在swift3中更改导航控制器上的后退按钮标题?

如何更改导航栏后退按钮的字体和颜色

swift - 导航控制器删除后退按钮箭头

更改后退导航栏按钮的字体

UIPageViewController 更改导航栏后退按钮标题

无法更改导航项后退按钮的文本