Search code examples
swiftuinavigationcontroller

NavBar not shown well Swift


I don't know why my navigation bar don't shown well. In the picture below, you can see the space between safe area and my navigation bar.

How I can resolve it?

here my custom tabBar

class TabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupBar()

    }
    
    // MARK: - Tab Setup
    
    private func setupBar() {
        self.setViewControllers([travelListNC(), mapNC()], animated: true)
    }
    
    private func travelListNC() -> UINavigationController {
        let storyboard = UIStoryboard(name: "TravelListViewController", bundle: Bundle.main)
        let travelsVC = storyboard.instantiateViewController(withIdentifier: "TravelListViewController") as! TravelListViewController
        travelsVC.tabBarItem.title = "Travels"
        travelsVC.tabBarItem.image = UIImage(systemName: "list.dash")
        return UINavigationController(rootViewController: travelsVC)
    }
    
    private func mapNC() -> UINavigationController {
        let storyboard = UIStoryboard(name: "MapViewController", bundle: Bundle.main)
        let mapVC = storyboard.instantiateViewController(withIdentifier: "MapViewController") as! MapViewController
        mapVC.title = "Map"
        mapVC.tabBarItem.title = "Map"
        mapVC.tabBarItem.image = UIImage(systemName: "map.fill")
        return UINavigationController(rootViewController: mapVC)
    }

}

when I call this tab bar

func homeView() {
        let tabBarController = TabBarController()
        let navController = UINavigationController(rootViewController: tabBarController)
        navController.modalPresentationStyle = .fullScreen
        present(navController, animated: true, completion: {
            let navController = tabBarController.viewControllers![0] as! UINavigationController
            let vc = navController.topViewController as! TravelListViewController
            vc.userLoged = self.userLoged
        })
    }

I set in the travelVC the title, which is a navigation title I suppose.

screen image


Solution

  • Your TabBarController is wrapped in a UINavigationController and so is your TravelListViewController, giving you the extra white space. Remove the UINavigationController from your TabBarController init.