Cannot Set Navigation Bar Font in Swift on Latest Xcode Version
Whoops. I figured this out on my own:
I needed an exclamation point following my declaration of the NSFontAttributeName as it requires a type "NSString!". Perhaps it only required an "NSString" before, but I have no issues now.
Working line:
NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 24)!
Working full code:
override func viewDidAppear(animated: Bool) {
self.navigationController?.navigationBar.topItem?.title = "Home"
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
}
Seems like a silly question now. Hope this helps someone else!
You are using UIFont(name:
intializer as it is defined as
init?(name fontName: String, size fontSize: CGFloat) -> UIFont
failable intializer read more from link.So it is returning optional.You need to unwrap it as it require AnyObject
not optional.
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
Thanks Benji!!!
I changed it a bit and applied it to the appearance attribute of the navigation controller.
var navigationBarAppearance = UINavigationBar.appearance()
navigationBarAppearance.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "OpenSans", size: 16)!, NSForegroundColorAttributeName: UIColor.whiteColor()]