1

In my main UIViewController embed in UINavigationController I have add an UILabel to a navigationBar using that code:

    if let navigationBar = self.navigationController?.navigationBar {

        let frameDomanda = CGRect(x: navigationBar.frame.width/2 - domandaN.frame.width/2, y: -10, width: domandaN.frame.width, height: navigationBar.frame.height)

        domandaN.frame = frameDomanda
        let secondLabel = UILabel(frame: secondFrame)
        secondLabel.text = "Second"

        navigationBar.addSubview(domandaN)
    }

But when I change Controller the UILabel is fixed. It doesn't disappear so I've added that code:

override func viewDidDisappear(animated: Bool) {
    domandaN.removeFromSuperview()
}

It works but I want it to disappear immediately after the press of the back button. Not like this image: (The "example" text goes away later)

enter image description here

2 Answers2

0

You can use viewWillDisappear, and don't forget to call super:

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    // you code here
}
shpasta
  • 1,913
  • 15
  • 21
  • Whats the difference if I don't write super.viewWillDisappear(animated) ? –  Jan 19 '16 at 19:21
0

Just add it in viewWillDisappear instead this:

override func viewWillDisappear(animated: Bool) {
    domandaN.removeFromSuperview()
}

For the animation parameter:

If true, the disappearance of the view is being animated.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • in viewWillDisappear the parameter animated doesn't exist –  Jan 19 '16 at 21:34
  • @Sarita check the [reference](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instm/UIViewController/viewWillDisappear:). `func viewWillDisappear(_ animated: Bool)`. – Rashwan L Jan 19 '16 at 21:37