swift 修改detailNavigationBar,但也修改rootNavigationBar

z6psavjg  于 2023-05-21  发布在  Swift
关注(0)|答案(1)|浏览(89)

我在将ViewController从MainViewController推到DetailViewController时遇到问题。我在MainViewController中的navigationBar是白色的,但当我将DetailViewController中的navigationBar项更改为白色或backgrondColor以清除时,它在MainViewController中也会更改。它就像视图是一样的,但只推背景...
主视图x1c 0d1x
详细视图

再次主视图

我正在以编程方式编写我的应用程序(没有故事板)。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控制器中的navigationBar从白色变为clearColor...
有什么问题吗?请帮帮我!

ni65a41a

ni65a41a1#

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

相关问题