添加 tabBarController、iOS、Swift 后导航消失

问题描述

我正在尝试实现 tabBarController 并且我设法做到了,但是现在我从 tabBar 内的屏幕导航消失了。我在 viewDidLoad 中的每个 viewController 中尝试将 isHidden 设置为 false,但没有帮助。下面的代码来自我的 tabBarController。

class BottomTabBarViewController: UITabBarController,UINavigationControllerDelegate {
    
override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.isNavigationBarHidden = false
    self.viewControllers = [bookedTrips,bookedTripsDetails,needHelp]
    customizeTabBar()
}

func customizeTabBar() {
    tabBar.backgroundColor = .init(red: 0.949,green: 0.949,blue: 0.949,alpha: 1)
    tabBar.shadowImage = UIImage()
    tabBar.backgroundImage = UIImage()
    tabBar.tintColor = .init(red: 0.949,green: 0.392,blue: 0.153,alpha: 1)
    tabBar.unselectedItemTintColor = .init(red: 0.753,green: 0.753,blue: 0.753,alpha: 1)
}

lazy public var bookedTrips: BookedTripsListViewController = {
    let initialTabBar = BookedTripsListViewController(networkManager: NetworkManager())
    let tabBarItems = UITabBarItem(title: "First",image: UIImage(named: "bag.png"),selectedImage: UIImage(named: "bag.png"))
    let tabBarItem = UITabBarItem(title: tabBarItems.title,image: tabBarItems.image,selectedImage: tabBarItems.selectedImage)
    initialTabBar.tabBarItem = tabBarItem
    return initialTabBar
}()

lazy public var bookedTripsDetails: TripDetailsViewController = {
    let initialTabBar = TripDetailsViewController()
    let tabBarItems = UITabBarItem(title: "Second",selectedImage: tabBarItems.selectedImage)
    initialTabBar.tabBarItem = tabBarItem
    return initialTabBar
}()

lazy public var needHelp: NeedHelpViewController = {
    let initialTabBar = NeedHelpViewController()
    let tabBarItems = UITabBarItem(title: "Third",selectedImage: tabBarItems.selectedImage)
    initialTabBar.tabBarItem = tabBarItem
    return initialTabBar
}()
}

不确定是否相关,但我将tabBar设置为登录后可见,因此如果登录数据正确,则显示tabBar。

解决方法

设法解决了这个问题,如果有人需要类似的帮助,解决方案是设置返回类型 UINaviagtionController 并将 viewController 的实例设置为 rootController,示例如下。

   lazy public var bookedTrips: UINavigationController = {
    let initialTabBar = UINavigationController(rootViewController: BookedTripsListViewController(networkManager: NetworkManager()))
    let tabBarItems = UITabBarItem(title: "First",image: UIImage(named: "bag.png"),selectedImage: UIImage(named: "bag.png"))
    let tabBarItem = UITabBarItem(title: tabBarItems.title,image: tabBarItems.image,selectedImage: tabBarItems.selectedImage)
    initialTabBar.tabBarItem = tabBarItem
    return initialTabBar
}()