呈现自定义选项卡视图控制器但出现黑屏
Presenting a custom tab view controller but getting a black screen
我的目标是显示一个选项卡视图控制器,该控制器管理多个选项卡,这些选项卡由包含视图控制器的导航控制器组成。
我在 AppDelegate 中将选项卡视图控制器 BaseTabBarController
设置为 window 我的根视图控制器。我的自定义选项卡视图控制器如下所示:
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
}
}
我的自定义导航控制器 class 是导航控制器的一个空子class。
问题是应用程序显示标签栏几分之一秒,然后立即变成黑屏(控制台消息:"Presenting view controllers on detached view controllers is discouraged")。我做错了什么?
你代码的其他部分一定有问题。当我使用你的代码并像这样使用它时,一切都按预期工作:
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
}
我的目标是显示一个选项卡视图控制器,该控制器管理多个选项卡,这些选项卡由包含视图控制器的导航控制器组成。
我在 AppDelegate 中将选项卡视图控制器 BaseTabBarController
设置为 window 我的根视图控制器。我的自定义选项卡视图控制器如下所示:
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
}
}
我的自定义导航控制器 class 是导航控制器的一个空子class。
问题是应用程序显示标签栏几分之一秒,然后立即变成黑屏(控制台消息:"Presenting view controllers on detached view controllers is discouraged")。我做错了什么?
你代码的其他部分一定有问题。当我使用你的代码并像这样使用它时,一切都按预期工作:
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
}