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
方法。这有用吗?
答案 0 :(得分:3)
您可以通过沿着这些行执行某些操作来更改应用程序委托中的字体,但这会更改整个应用程序中的字体,而不是在一个viewcontroller中。
if let customFont = UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20.0) {
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont], for: .normal)
}
答案 1 :(得分:2)
对于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)
}
<强>故事板强>
<强>输出强>
答案 2 :(得分: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)
答案 3 :(得分:0)
在 Swift 5 中,您可以执行以下操作:
let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: .regular)]
self.navigationItem.backBarButtonItem?.setTitleTextAttributes(attributes, for: .normal)
请注意,它将对下一个推送视图控制器有效,而不是显示屏上的当前视图控制器,这就是为什么它非常令人困惑!
此外,检查情节提要,然后选择上一个视图控制器的导航项,然后在后退按钮(检查器)中键入一些内容。
答案 4 :(得分:0)
要使其在 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
}