如何删除自定义导航栏后退按钮

时间:2016-01-26 20:18:22

标签: swift uinavigationcontroller

我在主视图控制器中添加了一些子视图。当我添加其中一个子视图时,我创建了一个自定义后退按钮,以便它将返回到主视图控制器,而不是导航堆栈中的先前视图控制器。我可以通过编程方式添加它,但我似乎无法弄清楚如何删除它。非常感谢任何帮助!

    func createCustomBackButton() {

              self.navigationItem.hidesBackButton = true
              let customFont = UIFont.systemFontOfSize(26.0)
              UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont], forState: UIControlState.Normal)

              // customBackButton is a property I set for UIBarButtonItem
              customBackButton = UIBarButtonItem(title: "<", style: .Plain, target: self, action: "back:")
              self.navigationItem.leftBarButtonItem = customBackButton
        }


           func back(sender: UIBarButtonItem) {
              UIView.animateWithDuration(0.3, animations: {
                 self.containerView.alpha = 0
                 }, completion: { finished in
                    self.view.sendSubviewToBack(self.containerView)
                    self.navigationItem.hidesBackButton = false

                    // what do I do on this line to get this to disappear or set to nil??
                    self.customBackButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.z, target: self, action: nil)

                    // clears out prior view that was there to free up memory
                    for view in self.containerView.subviews {

                       view.removeFromSuperview()
                    }
              })
           }

2 个答案:

答案 0 :(得分:1)

使用self.navigationItem.leftBarButtonItem = nil会将leftBarButtonItem重置为默认值。

答案 1 :(得分:0)

你应该使用展开segue。如果您正在使用故事板,请从&#34;目标视图控制器&#34;看起来像你要放松的VC的横向分享按钮(退出按钮)的东西。无论你喜欢什么,我们都可以称之为&#34;放松&#34;

然后调用同一个VC:

self.performSegueWithIdentifier("unwind", sender: self)

在VC中你要放松回来:

@IBAction func unwindToMapSegue(segue: UIStoryboardSegue) {
    // do something
}