Use UITabBarViewController in portrait but UISplitViewController in landscape in a Universal app

血红的双手。 提交于 2019-12-03 16:16:15
PLJNS

I've worked out how to do this, and while it doesn't contain solution anyone needs, I have included the code below which you can customise to your specific needs:

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!