NavigationBar titleView跳转到viewWillAppear

时间:2016-04-08 23:20:08

标签: swift ios9 navigationbar uinavigationitem

我创建了NavTitleView.xib(在此xib 2标签中添加了#34; TITLE"以及" SUBTITLE")并且还为此xib swift文件创建。

然后我想使用这个xib作为navigationItem.titleView

在viewWillAppear中我写了这段代码

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
        let titleView = NSBundle.mainBundle().loadNibNamed("NavTitleView", owner: self, options: nil)[0] as! NavTitleView
        titleView.TitleLabel.text = "My beautiful title"
        titleView.SubtitleLabel.text = "My beautiful subtitle"
        self.navigationItem.titleView = titleView

}

一切都很棒!但是当我的视图出现时 - 这个NavTitleView从左侧跳到中心!我不明白为什么?如何解决?

1 个答案:

答案 0 :(得分:0)

如果您设置了rightBarButtonItem,请尝试在self.navigationItem.rightBarButtonItem = UIBarButton内再次呼叫viewWillAppear