RobertJoseph
RobertJoseph

Reputation: 8158

viewDidAppear() called before view controller is pushed onto navigation stack

I have a basic navigation setup in my Storyboard: a vanilla UIViewController embedded in a UINavigationController. In my main VC I have two buttons that each segue to a UIViewController subclass: LabelledVC. In the subclass's viewDidAppear(_:) method I set the navigation item's titleView to a custom image:

class LabelledVC: UIViewController {

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

    let logoImage = UIImage(named: "apple")
    let logo = UIImageView(image: logoImage)

    logo.contentMode = .scaleAspectFit
    logo.frame = CGRect(x: 0, y: 0, width: 32, height: 32)

    navigationItem.titleView = logo
  }

}

For some reason LabelledVC's viewDidAppear(_:) method is being called when the app loads (before it is pushed onto the navigation stack) which doesn't make any sense to me. You can find the project here.

enter image description here

Upvotes: 1

Views: 645

Answers (1)

Sergey
Sergey

Reputation: 1617

Your MainVC is inherit from LabelledVC. So when application did show this controller the system calling viewDidAppear in ViewController but you don't have implementation for this method, so system call this method from parent class.

enter image description here

One other thing. For your example best place to configure NavigationItem is viewDidLoad method.

Upvotes: 2

Related Questions