无法在最新的Xcode版本上的Swift中设置导航栏字体 [英] Cannot Set Navigation Bar Font in Swift on Latest Xcode Version

查看:55
本文介绍了无法在最新的Xcode版本上的Swift中设置导航栏字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在更新到Xcode 6.1以便与iOS 8.1保持一致之前,以下代码运行良好:

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

问题具体在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"

我在另一个StackOverflow问题上找到了此原始代码,并且在此更新之前它一直按预期运行(昨天下载了该代码).我的字体确实已正确安装.

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?

谢谢!

推荐答案

糟糕.我自己解决了这个问题:

Whoops. I figured this out on my own:

我在声明NSFontAttributeName之后需要一个感叹号,因为它需要类型为"NSString!".也许以前只需要一个"NSString",但是现在我没有问题了.

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.

工作线:

NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 24)!

完整的代码:

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!

这篇关于无法在最新的Xcode版本上的Swift中设置导航栏字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆