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

问题描述 投票:0回答:5

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

ios swift fonts uinavigationcontroller
5个回答
14
投票

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

}

故事板

输出


4
投票

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

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

4
投票

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

0
投票

代替:

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)

0
投票

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

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

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

此外,检查情节提要并选择 previous 视图控制器的导航项,然后在 后退按钮(检查器)中键入内容。

© www.soinside.com 2019 - 2024. All rights reserved.