Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing UINavigationBar title in iOS8 Swift

I have programmatically added UINavigationBar in my Superview called BaseViewController which inherits UIViewController. But When I try to set a title to the UINavigationBar. I get the error called fatal error: Can't unwrap Optional.None

Below is my swift class. How can unwrap and change the UINavigationBar title

import UIKit

class BaseViewConroller: UIViewController {
    var navBar:UINavigationBar=UINavigationBar()

    override func viewDidLoad() {
        super.viewDidLoad()
        setNavBarToTheView()
        // Do any additional setup after loading the view.
        navBar.topItem.title="test test"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func setNavBarToTheView()
    {
        navBar.frame=CGRectMake(0, 0, 320, 50)
        navBar.backgroundColor=(UIColor .blackColor())
        self.view .addSubview(navBar)
    }

}
like image 950
Ashan Avatar asked Jun 24 '14 09:06

Ashan


1 Answers

Replace your navBar.topItem.title="test test" with self.title="test test". Cheers!!

like image 111
Rocky Perera Avatar answered Oct 31 '22 01:10

Rocky Perera