Swift - 带有 TabBar 的 NavigationBar 不能正常工作

Swift - NavigationBar with TabBar not working well

就我而言,我正在制作一个 real-time 聊天应用程序。我在主视图中使用大标题和搜索栏。但是,我想将 tabBar 添加到我的应用程序 navigationController 中,但 tabBar 无法正常工作。

注意:我正在用代码做所有事情,请不要告诉我故事板。

这是应该的:

添加 tabBar 时发生了什么:

AppDelegate.swift:

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

    UIApplication.shared.statusBarStyle = .lightContent

    FirebaseApp.configure()
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    window?.rootViewController = UINavigationController(rootViewController: TabBarController())

    return true
}

TabBarController.swift:

override func viewDidLoad() {
    super.viewDidLoad()

    let messagesController = UINavigationController(rootViewController: MessagesController())
    messagesController.tabBarItem.title = "Sohbetler"
    messagesController.tabBarItem.image = UIImage(named: "chats")

    viewControllers = [messagesController]
}

你把两个导航一个用于标签栏,第二个用于控制器隐藏一个导航栏,你的问题就会解决

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

    UIApplication.shared.statusBarStyle = .lightContent

    FirebaseApp.configure()
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()
    let objNav = UINavigationController(rootViewController: TabBarController())
    objNav.isNavigationBarHidden = true
    window?.rootViewController = objNav

    return true
}

在 viewcontroller 您想要显示标签栏的地方尝试以下行

self.tabBarController?.tabBar.isHidden = false