为什么我的UIBarButton没有显示在导航栏中? (可能的iOS 11错误?)

时间:2017-09-28 22:48:36

标签: ios swift uinavigationbar

如果我对VC执行segues,则会显示UIBarButtons。但是,如果我以编程方式推送到视图控制器,则UIBarButtons将不会显示。 iOS 9或iOS 10中不存在错误。

此代码存在于应用程序打开时显示的第一个视图控制器中。我正在检查UserDefaults以查看是否应该恢复我们称之为“交互”的内容,并且我正在以编程方式创建堆栈。由于我在恢复应用程序状态时遇到的问题,我已经恢复了这种方法。

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

      if UserDefaults.standard.bool(forKey: "ShouldRestoreInteraction") {
            if
                let uri = UserDefaults.standard.url(forKey: "InteractionObjectURI"),
                let id = CoreDataUtils.coord.managedObjectID(forURIRepresentation: uri),
                let interactionObject = CoreDataUtils.context.object(with: id) as? InteractionObject
            {
                let sb = UIStoryboard(name: "Interaction", bundle: nil)
                let interactionCreateOrEdit = sb.instantiateViewController(withIdentifier: "InteractionUnified") as! InteractionCreateOrEdit
                interactionCreateOrEdit.interaction = Interaction(object: interactionObject)
                interactionCreateOrEdit.interactionObject = interactionObject

                if UserDefaults.standard.bool(forKey: "RestoreToDashboard") {
                    // restore to dashboard
                    let sb = UIStoryboard(name: "MyDashboard", bundle: nil)
                    let dashNav = sb.instantiateInitialViewController() as! UINavigationController
                    let dashboardTVC = sb.instantiateViewController(withIdentifier: "DashboardTableViewController")

                    self.revealViewController().setFront(dashboardTVC, animated: false)

                    dashNav.pushViewController(dashboardTVC, animated: false)
                    dashNav.pushViewController(interactionCreateOrEdit, animated: false)

                    navigationController!.present(dashNav, animated: false, completion: nil)
                } else {
                    print("Error restoring interaction - did not specify bottom controller in stack")
                }
            } else {
                print("Error restoring interaction - object in core data no longer exists")
            }
        } 
    }

然而,奇怪的是如果我单击“Debug View Hierarchy”,则UIBarButtons将显示在手机上,但不会显示在Debug View Hierarchy中。如果我继续执行程序,然后再次点击Debug View Hierarchy,按钮将显示在DVH中。此外,在继续执行程序后,UIBarButtons将继续显示在导航栏中。

1 个答案:

答案 0 :(得分:1)

看起来马特先生的“保证”是正确的。不确定为什么我的代码适用于以前版本的iOS ...无论如何解决方案如下:

if UserDefaults.standard.bool(forKey: "RestoreToDashboard") {
 // restore to dashboard
 let sb = UIStoryboard(name: "MyDashboard", bundle: nil)
 let dashNav = sb.instantiateInitialViewController() as! UINavigationController
 let dashboardTVC = sb.instantiateViewController(withIdentifier:"DashboardTableViewController")
 dashNav.setViewControllers([dashboardTVC, interactionCreateOrEdit], animated: false)
 self.revealViewController().setFront(dashNav, animated: false)
}