UIViewController后退按钮未显示

时间:2018-12-28 03:09:01

标签: ios swift

rootViewController-> tabviewcontroller-> viewController1-> viewController2

按这种顺序,后退按钮可以正常工作,但是当viewController2弹出回到viewController1时,viewController1的后退按钮没有显示,因此我无法返回tabviewcontroller。

可能的原因是什么?感谢您的帮助。

这是我的故事板

enter image description here

//after viewWillAppear
func initNavigation(){
    //topItem is self.navigationController?.navigationBar.topItem
    guard topItem != nil else {
        return
    }
    //custom letButtonBar just make tabviewcontroller title at left
    //other viewcontroller title is "" so position still at center
    let leftButton = UIBarButtonItem(title:  "", style:UIBarButtonItem.Style.plain, target: self, action: nil)
    let attr = [NSAttributedString.Key.foregroundColor:UIColor.white, NSAttributedString.Key.font: UIFont(name: "iconfont", size: UIFont.buttonFontSize)!]

    leftButton.setTitleTextAttributes(attr, for: .normal)
    leftButton.setTitleTextAttributes(attr, for: .highlighted)
    leftButton.setTitleTextAttributes(attr, for: .disabled)
    leftButton.tintColor = UIColor.white
    leftButton.isEnabled = false

    topItem?.backBarButtonItem?.tintColor = UIColor.white
    topItem?.setLeftBarButton(leftButton, animated: true)
    topItem?.title = ""

}
//tabviewcontroller:
override func initNavigation() {
    super.initNavigation()
    topItem?.leftBarButtonItem?.title = "tabviewcontrollerTitleAtLeft"
}
//viewcontroller1
 override func initNavigation() {
    super.initNavigation()
    self.navigationItem.title = "viewController1TitleAtCenter"
}
//viewcontroller2
 override func initNavigation() {
    super.initNavigation()
    self.navigationItem.title = "viewController2TitleAtCenter"
}   

我尝试了很多次,找到了解决方案,我在viewcontroller1中删除了super.initNavigation(),最后仍然可以正常工作,仍然不知道原因。

2 个答案:

答案 0 :(得分:0)

似乎在VC之间可以有另一个UINavigationController,如下所示:

enter image description here

隐藏第一个导航栏。并使用第二个导航栏作为根。

答案 1 :(得分:0)

您应该检查如何呈现和弹出VC。可能是由与此有关的某些东西引起的。