user3746428
user3746428

Reputation: 11175

Setting navigation bar text font in Xcode 6.1

I am using a non-default font in my apps navigation bar, and this worked absolutely fine prior to Xcode 6.1. Now I get an error on the line of code where I am defining the font type and colour.

This is my code:

    var attributes = [NSForegroundColorAttributeName: UIColor.whiteColor(),NSFontAttributeName: UIFont(name: "Avenir", size: 24)]
    self.navigationController?.navigationBar.titleTextAttributes = attributes

What do I need to change to get this to work again?

Upvotes: 1

Views: 819

Answers (1)

James Chen
James Chen

Reputation: 10874

UIFont(name:size:) returns an optional, which cannot be used as the value.Change it to this:

var attributes = [NSForegroundColorAttributeName: UIColor.whiteColor(),NSFontAttributeName: UIFont(name: "Avenir", size: 24)!]

By doing this you unwrap the UIFont? value to NSFontAttributeName. I believe you have to make sure the font is actually there to avoid crash.

Upvotes: 2

Related Questions