swift - 修改detailNavigationBar但也修改rootNavigationBar

标签 swift uinavigationcontroller uinavigationbar pushviewcontroller

将 ViewController 从 MainViewController 推送到 DetailViewController 时遇到问题。我的 MainViewController 中的导航栏是白色的,但是当我将 DetailViewController 中的导航栏项目更改为白色或背景颜色以清除时,它在 MainViewController 中也会发生变化。就像 View 是一样的,但只推送背景......

主视图 enter image description here

详细 View enter image description here

再次主视图 enter image description here

我正在以编程方式编写我的应用程序(没有 Storyboard)。 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 Controller 时,MainView Controller 中的导航栏从白色更改为透明颜色...

有什么问题吗?请帮忙!

最佳答案

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

关于swift - 修改detailNavigationBar但也修改rootNavigationBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46707863/

相关文章:

ios - Realm swift : Can't get an object from realm

swift - 如何使用字典将 NSButton 映射到 Int

iPad UINavigationController 横向问题

ios - 如何在 Swift 中识别我们来自哪个 View Controller

iphone - 带有自定义后退按钮的 UINavigationController 子类

swift - locationManager 函数在代码末尾触发

swift - 将文本制作成数组 - Swift

swift - 如何以编程方式创建 Unwind segue

ios - 如何删除导航栏底部颜色 iOS 7

ios - 尝试处理 iOS 中的 "back"导航按钮操作