“NavBar not shown well Swift” can be translated to: “导航栏未正常显示 Swift”

huangapple go评论64阅读模式
英文:

NavBar not shown well Swift

问题

我不知道为什么我的导航栏显示不正常。
在下面的图片中,您可以看到安全区域和我的导航栏之间的空间。

我该如何解决这个问题?

这是我的自定义 tabBar:

class TabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupBar()
    }
    
    // MARK: - Tab 设置
    
    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 = "旅行"
        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 = "地图"
        mapVC.tabBarItem.title = "地图"
        mapVC.tabBarItem.image = UIImage(systemName: "map.fill")
        return UINavigationController(rootViewController: mapVC)
    }
}

当我调用这个 tabBar 时:

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
    })
}

我在 travelVC 中设置了标题,这应该是一个导航标题。

英文:

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

答案1

得分: 0

Remove the UINavigationController from your TabBarController init.

英文:

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.

huangapple
  • 本文由 发表于 2023年7月6日 20:04:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定