繁体   English   中英

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

[英]Change the font on the back button of a navigation controller under swift 4

斯威夫特 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方法中。 这应该工作吗?

对于 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)

}

故事板

在此处输入图片说明

输出

在此处输入图片说明

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

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

而不是:

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)

Swift 5 中,您可以通过以下方式实现:

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

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

此外,检查故事板并选择前一个视图控制器的导航项,然后在后退按钮(检查器)中键入一些内容。

要使其在 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
}

暂无
暂无

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

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