. KVO.
, viewWillAppear (_:), navigationBar isHidden . viewDidAppear (_:), navigationBar isHidden false. , . , KVO.
viewWillAppear (_:)
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.addObserver(self, forKeyPath: "hidden", options: [.new], context: nil)
}
navigationBar isHidden observerValue()
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard let keyPath = keyPath else { return }
if keyPath == "hidden" {
// you should remove the KVO before set
navigationController?.navigationBar.removeObserver(self, forKeyPath: "hidden")
navigationController?.navigationBar.isHidden = true
} else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
}
, .
, .