问题描述
我使用的UITabBarController具有超过5个项目。我自定义了moreNavigationController来删除导航栏并使它变暗。但是,当我将设备旋转到横向并再次进入肖像时,它会在顶部显示导航栏,并再次显示其白色。仅当我从moreNavigationController中选择一个ViewController并将其旋转时,这种情况才会发生。
这是我的代码:
class CustomTabBar: UITabBarController,UITabBarControllerDelegate {
override func viewDidLoad() {
delegate = self
self.customizableViewControllers = []
self.moreNavigationController.navigationBar.isHidden = true
}
func tabBarController(_ tabBarController: UITabBarController,didSelect viewController: UIViewController) {
// style all the tab bar windows and the More tab bar tableview
if viewController == moreNavigationController,let moreTableView = moreNavigationController.topViewController?.view as? UITableView {
moreNavigationController.setNavigationBarHidden(true,animated: false)
let dark = UIColor(hexString: "#1F242A")
moreTableView.backgroundColor = dark
moreTableView.tintColor = .white
moreTableView.visibleCells.forEach{ cell in
cell.backgroundColor = dark
cell.textLabel?.textColor = .white
}
}
}
解决方法
使用以下代码实现了这一点。我之前使用过viewWillTransition,但关键部分是在过渡后之后运行自定义代码。
override func viewWillTransition(to size: CGSize,with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size,with: coordinator)
coordinator.animate(alongsideTransition: nil) { _ in
self.moreNavigationController.setNavigationBarHidden(true,animated: false)
}
}