我想在iPhone上使用UITabBarViewController时使用UITabBarViewController,在iPhone 6/6 +上使用UISplitViewController时可以使用横向或iPad。在iPad或iPhone 6/6 +横向上时,标签栏项目在最顶层的视图控制器中显示为UITableViewCells。
我真的希望调用对象不必担心设备或方向,而是将视图控制器作为输入传递到iPhone上的标签栏中的纵向和iPad和iPhone 6上的根视图控制器中/ 6 +在风景中。
这是我的意思的直观表示:
我认为可以使用标签栏图像属性将图像放置在表格视图单元格中。我在Swift中这样做,我认为这个策略可能是这样的:
class ResponsiveRootViewController: UIViewController {
var viewControllers: NSArray?
var responsiveTabBarController: UITabBarController?
var responsiveSplitViewController: UISplitViewController?
var responsiveRootViewController: UITableViewController?
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
}
}
这可能吗?这已经完成了吗?我应该改为实现一个标签栏视图控制器,它在横向上隐藏它的标签栏(这对我来说似乎超级hacky)?当方向更改时,如何将生成的根表视图控制器(我希望标签栏项存在的控制器)添加到视图层次结构中?
答案 0 :(得分:4)
我已经找到了如何做到这一点,虽然它不包含任何人需要的解决方案,但我已经包含了下面的代码,您可以根据自己的特定需求进行自定义:
SplitTabBarViewController.swift
var rootViewControllers: [UIViewController]
var detailViewController: UIViewController
var aTabBarController: UITabBarController
var aSplitViewController: UISplitViewController
var primaryTabBarTableViewController: SplitTabBarPrimaryTableViewController
init(rootViewControllers: [UIViewController], detailViewController: UIViewController) {
self.rootViewControllers = rootViewControllers
self.detailViewController = detailViewController
self.aTabBarController = UITabBarController()
self.aTabBarController.viewControllers = self.rootViewControllers
self.aSplitViewController = UISplitViewController()
self.primaryTabBarTableViewController = SplitTabBarPrimaryTableViewController(rootViewControllers: self.rootViewControllers)
self.aSplitViewController.viewControllers = [self.primaryTabBarTableViewController, self.detailViewController]
super.init(nibName: nil, bundle: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
if (UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation)) {
self.addSplitViewController()
} else {
self.addTabBarController()
}
}
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
self.removeTabBarController()
self.addSplitViewController()
} else {
self.removeSplitViewController()
self.addTabBarController()
}
}
func addTabBarController() {
self.view.addSubview(self.aTabBarController.view)
self.addChildViewController(self.aTabBarController)
self.aTabBarController.view.frame = self.view.frame
}
func removeTabBarController() {
self.aTabBarController.removeFromParentViewController()
self.aTabBarController.view.removeFromSuperview()
}
func addSplitViewController() {
self.view.addSubview(self.aSplitViewController.view)
self.addChildViewController(self.aSplitViewController)
self.aSplitViewController.view.frame = self.view.frame
}
func removeSplitViewController() {
self.aSplitViewController.removeFromParentViewController()
self.aSplitViewController.view.removeFromSuperview()
}
SplitTabBarPrimaryTableViewController.swift
var rootViewControllers: [UIViewController]
init(rootViewControllers: [UIViewController]) {
self.rootViewControllers = rootViewControllers;
super.init(nibName: nil, bundle: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "reuseIdentifier")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.rootViewControllers.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = self.rootViewControllers[indexPath.row].title
return cell
}