我的目标是显示一个标签视图控制器,该控制器管理由包含视图控制器的导航控制器组成的多个标签。
我将选项卡视图控制器BaseTabBarController
设置为AppDelegate中的根视图控制器窗口。我的自定义选项卡视图控制器如下所示:
class BaseTabBarController: ESTabBarController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
let v1 = BaseNavigationController(rootViewController: SubscriptionsController())
let v2 = BaseNavigationController(rootViewController: SubscriptionsController())
v1.tabBarItem = ESTabBarItem(title: "Home", image: #imageLiteral(resourceName: "tab_bar_home"), selectedImage: #imageLiteral(resourceName: "tab_bar_home"))
v2.tabBarItem = ESTabBarItem(title: "Home", image: #imageLiteral(resourceName: "tab_bar_home"), selectedImage: #imageLiteral(resourceName: "tab_bar_home"))
self.viewControllers = [v1, v2]
self.hidesBottomBarWhenPushed = true
}
}
我的自定义导航控制器类是导航控制器的空子类。
问题是应用程序显示标签栏几分之一秒,并立即变成黑屏(控制台消息:“不建议在分离的视图控制器上显示视图控制器”)。我做错了什么?
答案 0 :(得分:1)
代码的其他部分出现了问题。当我接受你的代码并像这样使用它时,一切都按预期工作:
class BaseTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
let v1 = UINavigationController(rootViewController: UIViewController())
let v2 = UINavigationController(rootViewController: UIViewController())
v1.tabBarItem = UITabBarItem(title: "Home", image: nil, selectedImage: nil)
v2.tabBarItem = UITabBarItem(title: "Home", image: nil, selectedImage: nil)
self.viewControllers = [v1, v2]
self.hidesBottomBarWhenPushed = true
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = BaseTabBarController()
window?.makeKeyAndVisible()
return true
}