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)
}
}
Replace your navBar.topItem.title="test test"
with self.title="test test"
. Cheers!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With