在更新到 Xcode 6.1 以跟上 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)
我在这里遇到的错误是:
! "Could not find an overload for 'init' that accepts the supplied arguments"
我在另一个 StackOverflow 问题上找到了这个原始代码,并且在这次更新之前它按预期工作(昨天下载)。我的字体确实安装正确。
我现在应该以不同的方式编写此代码,还是应该有一种全新的方式来设置导航栏字体?
Thanks!
哎呀。我自己想出了这个:
我在声明 NSFontAttributeName 后需要一个感叹号,因为它需要类型“NSString!”。也许以前只需要一个“NSString”,但现在没有问题了。
工作线:
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()]
}
现在看来是个愚蠢的问题。希望这对其他人有帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)