修改detailNavigationBar,但也修改了rootNavigationBar

时间:2017-10-12 10:57:02

标签: swift uinavigationcontroller uinavigationbar pushviewcontroller programmatically

当从MainViewController到DetailViewController的pushViewController时出现问题。我在MainViewController中的navigationBar是白色的,但当我将DetailViewController中的navigationBar项目更改为white或backgrondColor以清除时,它在MainViewController中也会发生变化。它就像视图一样,但只推动背景...

主视图 enter image description here

详情视图 enter image description here

主要观点再次 enter image description here

我是以编程方式编写我的应用程序(没有故事板)。 AppDelegate是这样的:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    /********** Storyboard Config **********/
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()
    window?.rootViewController = TabBarController()

    return true
}

TabBarController是这样的:

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

    // Create Tab MainViewController
    let mainNavigationController = MainViewController(collectionViewLayout: UICollectionViewFlowLayout())
    mainNavigationController.title = "Main"
    let mainController = UINavigationController(rootViewController: mainNavigationController)
    mainController.title = "Main"
    mainController.tabBarItem.image = UIImage(named: "UITabBarBookmarksTemplate")
    mainController.tabBarItem.selectedImage = UIImage(named: "UITabBarBookmarksTemplateSelected")

    // Create Tab UserViewController
    let userNavigationController = UserViewController(collectionViewLayout: UICollectionViewFlowLayout())
    userNavigationController.title = ""
    let userController = UINavigationController(rootViewController: userNavigationController)
    userController.title = "Profile"
    userController.tabBarItem.image = UIImage(named: "UITabBarContactsTemplate")
    userController.tabBarItem.selectedImage = UIImage(named: "UITabBarContactsTemplateSelected")

    viewControllers = [mainController, userController]

}

MainViewController的pushViewController代码是:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let layout = UICollectionViewFlowLayout()
    let controller = DetailViewController(collectionViewLayout: layout)
    controller.data = cloudDatas?[indexPath.item]

    navigationController?.pushViewController(controller, animated: true)
}

我在DetailViewController中唯一改变的是:

// Translucent Navigation Bar
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true
    self.navigationController?.navigationBar.backgroundColor = UIColor.clear
    self.navigationController?.navigationBar.tintColor = UIColor.white

使用此代码,当我导航到DetailViewController并返回MainView控制器时,MainView控制器中的navigationBar从白色变为clearColor ...

♂️

问题是什么?请帮忙!

谢谢!

1 个答案:

答案 0 :(得分:0)

DetailViewController中使用以下内容:

 self.navigationController?.navigationBar.isHidden = true

取代这个:

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.backgroundColor = UIColor.clear
self.navigationController?.navigationBar.tintColor = UIColor.white

并在MainViewControllerviewWillAppear中添加:

self.navigationController?.navigationBar.isHidden = false