Cannot Set Navigation Bar Font in Swift on Latest Xcode Version

久未见 提交于 2020-01-03 10:22:28

问题


The following code was running fine before updating to Xcode 6.1 to stay up with iOS 8.1:

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()]
}

The issue is specifically in NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)

and the error I'm getting here is:

! "Could not find an overload for 'init' that accepts the supplied arguments"

I found this original code on a different StackOverflow question and it was working as expected until this update (downloaded it yesterday). My font is indeed installed properly.

Should I be writing this code differently now, or is there an entirely new way in which I should set my Navigation Bar Font?

Thanks!


回答1:


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!




回答2:


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()]



回答3:


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()]



回答4:


It is better to declare your font as conditional , like this way :

 if let font = UIFont (name: "AdobeArabic-BoldItalic", size: 20) {
            UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: font]
        }

Doing this , makes sure that your font is already found , that I have installed new font , and when used it without conditional if , it issued an exception , of unwrapping an optional .




回答5:


override func viewWillLayoutSubviews() {

    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName : UIFont(name: "Roboto-Regular", size: 14)!]
}



回答6:


Swift 4.2

Change Small Title when layout scrolled down

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()]
}

Change Large Title

override func viewDidAppear(animated: Bool) {  
     self.navigationController?.navigationBar.topItem?.title = "Home"
     self.navigationController?.navigationBar.largeTitleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34),  NSForegroundColorAttributeName: UIColor.whiteColor()]
}


来源:https://stackoverflow.com/questions/26556583/cannot-set-navigation-bar-font-in-swift-on-latest-xcode-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!