我以编程方式在我的Superview中添加了UINavigationBar,名为BaseViewController,它继承了UIViewController . 但是当我尝试为UINavigationBar设置 Headers 时 . 我收到名为 fatal error: Can't unwrap Optional.None 的错误
以下是我的快速课程 . 如何解包和更改UINavigationBar Headers
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)
}
}
4 回答
将
navBar.topItem.title="test test"
替换为self.title="test test"
. 干杯!!以实际方式更改导航栏文本 . 只需调用下面写的func
setNavigationBarTitleText("WhateverTextNeeded")
即可 . 我假设您已经配置了UINavigationBar .我希望这有帮助 .
我有这个代码...我希望,这可以帮到你 .
您可以使用此代码转换导航栏 .