英文:
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.
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论