Hiding the master view controller with UISplitViewController in iOS8

后端 未结 10 968
囚心锁ツ
囚心锁ツ 2020-12-12 19:06

I have an iOS7 application, which was based on the Xcode master-detail template, that I am porting to iOS8. One area that has changed a lot is the UISplitViewControlle

10条回答
  •  Happy的楠姐
    2020-12-12 19:48

    I was able to have the desired behavior in a Xcode 6.3 Master-Detail Application (universal) project by adding the following code in the MasterViewController's - prepareForSegue:sender: method:

    if view.traitCollection.userInterfaceIdiom == .Pad && splitViewController?.displayMode == .PrimaryOverlay {
        let animations: () -> Void = {
            self.splitViewController?.preferredDisplayMode = .PrimaryHidden
        }
        let completion: Bool -> Void = { _ in
            self.splitViewController?.preferredDisplayMode = .Automatic
        }
        UIView.animateWithDuration(0.3, animations: animations, completion: completion)
    }
    

    The complete - prepareForSegue:sender: implementation should look like this:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showDetail" {
            if let indexPath = self.tableView.indexPathForSelectedRow() {
                let object = objects[indexPath.row] as! NSDate
                let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
                controller.detailItem = object
                controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
                controller.navigationItem.leftItemsSupplementBackButton = true
    
                if view.traitCollection.userInterfaceIdiom == .Pad && splitViewController?.displayMode == .PrimaryOverlay {
                    let animations: () -> Void = {
                        self.splitViewController?.preferredDisplayMode = .PrimaryHidden
                    }
                    let completion: Bool -> Void = { _ in
                        self.splitViewController?.preferredDisplayMode = .Automatic
                    }
                    UIView.animateWithDuration(0.3, animations: animations, completion: completion)
                }
            }
        }
    }
    

    Using traitCollection may also be an alternative/supplement to displayMode in some projects. For example, the following code also works for a Xcode 6.3 Master-Detail Application (universal) project:

    let traits = view.traitCollection
    if traits.userInterfaceIdiom == .Pad && traits.horizontalSizeClass == .Regular {
        let animations: () -> Void = {
            self.splitViewController?.preferredDisplayMode = .PrimaryHidden
        }
        let completion: Bool -> Void = { _ in
            self.splitViewController?.preferredDisplayMode = .Automatic
        }
        UIView.animateWithDuration(0.3, animations: animations, completion: completion)
    }
    

提交回复
热议问题