如何设置导航栏字体的颜色和大小

use*_*428 3 uinavigationbar swift

我一直在尝试在导航栏中设置文本的字体大小和字体颜色,但是我遇到了问题.

基本上似乎正在发生的是我的两行代码相互覆盖,我只能让它们自己工作.

这是我的代码:

    self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Avenir", size: 30)]
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
Run Code Online (Sandbox Code Playgroud)

我需要更改什么才能更改字体大小和颜色?

Ste*_*erg 12

在快速的Mandav中,可以通过这种方式实现出色的答案:

var attributes = [
    NSForegroundColorAttributeName: UIColor.greenColor(),
    NSFontAttributeName: UIFont(name: "Avenir", size: 30)!
]
self.navigationController?.navigationBar.titleTextAttributes = attributes
Run Code Online (Sandbox Code Playgroud)

请注意,UIFont初始化程序返回一个可选项,因为它可能无法找到字体,因此使用感叹号.