Reputation: 5490
I've read all the other responses to this, and they all seems really old and don't seem to work.
I'm trying to create an application with a UITabBar, and have on of those tabs be a UISplitViewController.
The setup is fairly simple:
let redViewController = UIViewController()
redViewController.view.backgroundColor = .red
let blueViewController = UIViewController()
blueViewController.view.backgroundColor = .blue
let splitViewController = UISplitViewController(style: .doubleColumn)
splitViewController.setViewController(redViewController, for: .primary)
splitViewController.setViewController(blueViewController, for: .secondary)
let tabBarController = UITabBarController()
tabBarController.setViewControllers([splitViewController], animated: false)
window?.rootViewController = tabBarController
Upvotes: 1
Views: 541
Reputation: 701
UISplitViewController
changed quite a bit in iOS 14 with the introduction of column-style layouts. Column-style split view controllers cannot be embedded in tab bar controllers, but classic style ones can. You create a classic style split view controller by using any initialiser except for init(style:)
.
For example:
let red: UIViewController = ...
let blue: UIViewController = ...
// Note that the pre-iOS 14 initialiser is being used
let splitViewController = UISplitViewController(nibName: nil, bundle: nil)
splitViewController.viewControllers = [red, blue]
splitViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .bookmarks, tag: 0)
let tabBarController = UITabBarController(nibName: nil, bundle: nil)
tabBarController.viewControllers = [splitViewController]
Note that you can't use any of the new API introduced in iOS 14 with classic style split views (eg the triple column layout, split behaviour). If you want to use this new API you'll have to use a sidebar instead of a tab bar.
I reported this problem in Feedback Assistant but apparently it's working as intended. I guess this is Apple's way of favouring sidebar based navigation over tab bar navigation in iPad apps.
Upvotes: 1