简体   繁体   中英

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 .

在此处输入图片说明

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.

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM