我的应用程序中有几个视图,我只想在其中一个上有一个navigationbar……我使用了一个navigationcontroller,一开始我使用的是这段代码(当时我的应用还处于起步阶段,只有2个视图)
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.setNavigationBarHidden(true, animated: animated)
super.viewWillAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
self.navigationController?.setNavigationBarHidden(false, animated: animated)
super.viewWillDisappear(animated)
}它工作得很好--但是,这个应用程序变得更加复杂了--我有这些观点
lazy var orderedViewControllers: [UIViewController] = {
return [self.newVc(viewController: "pageOne"),
self.newVc(viewController: "pageTwo"),
self.newVc(viewController: "pageThree"),
self.newVc(viewController: "pageFour"),
self.newVc(viewController: "activate")
]
}()即使我为每个视图创建了一个自定义的视图控制器,这段代码也不适用。
我认为这样做的方法是将顶部的代码块放在每个视图中,但这对底部的块不起作用。本质上,我的问题是如何使用NavigationController在一个view上创建一个条形图。
发布于 2018-12-06 03:09:21
一种选择是:使用“基视图控制器”类来隐藏/显示导航栏,并使"pages“子类成为”基类“的子类。
import UIKit
class BaseViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
}
class ViewController: UIViewController {
// has buttons with
// Show (e.g. push)
// segues to Settings, First, Second, Third view controllers
}
class SettingsViewController: UIViewController {
// Settings VC is a normal UIViewController, because
// we *want* the NavBar to remain visible
}
class FirstViewController: BaseViewController {
@IBAction func backTapped(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
}
}
class SecondViewController: BaseViewController {
@IBAction func backTapped(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
}
}
class ThirdViewController: BaseViewController {
@IBAction func backTapped(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
}
}发布于 2018-12-06 03:13:00
您可以使用UINavigationControllerDelegate的此方法
optional func navigationController(_ navigationController: UINavigationController,
willShow viewController: UIViewController,
animated: Bool){
if viewController == self."desired view controller" {
self.isNavigationBarHidden = true
}else{
self.isNavigationBarHidden = false
}
}https://stackoverflow.com/questions/53638716
复制相似问题