侧滑导航栏消失问题笔记

如果前页面和后页面不同,则会出现轻微侧滑时导航栏莫名其妙没了😂

Demo-PushAndPop

Demo-PushAndPop

[已解决] 隐藏导航栏+改变状态栏样式时出现导航栏莫名其妙没了

问题

snapshot

ViewController.swift实现

1
2
3
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

时,两个ViewController对应的UIStatusBarStyle不同,则会出现如上图。UIStatusBarStyle一致则正常。

解决关键

1
2
3
4
5
class NavigationController: UINavigationController {
override var childViewControllerForStatusBarStyle: UIViewController? {
return topViewController
}
}

只需要将childViewControllerForStatusBarStyle设置为UINavigationController.topViewController即可。

另外

附上BViewController中相关代码。在切换的两个UIViewControllerpreferredStatusBarStyle一样时,不需要设置UINavigationController.childViewControllerForStatusBarStyle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class BViewController: UIViewController {

var navigationBarHidden = false

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

guard let navigationController = navigationController else {
return
}
if navigationController.isNavigationBarHidden != navigationBarHidden {
navigationController.setNavigationBarHidden(navigationBarHidden, animated: animated)
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

guard let navigationController = navigationController else {
return
}
guard let last = navigationController.viewControllers.last as? BViewController else {
return
}
if last.navigationBarHidden != navigationBarHidden {
navigationController.setNavigationBarHidden(last.navigationBarHidden, animated: animated)
}
}
}

感谢

  • @木头 viewWillAppear更换为viewDidAppear然后调试,在Demo中测试也可以曲线救国。
  • @Harry 提供的终极大法~