简体   繁体   中英

Update frame of DetailViewController in subclass of UISplitViewController

I wish to change the frame of DetailViewController when splitViewController?.displayMode == .PrimaryOverlay , such that the DetailViewController view is never obscured by the MasterViewController . I have managed to get this working when navigating from splitViewController?.displayMode == .PrimaryHidden to splitViewController?.displayMode == .PrimaryOverlay in iPad portrait orientation, using a custom UISplitViewController with overriden viewDidLayoutSubviews method as described in jrc's answer here: Change the width of Master in UISplitViewController (see below). I have linked the UISplitViewController in storyboard to SplitViewController , and viewDidLayoutSubviews() is called on initial app load with iPad.

SplitViewController.swift

override func viewDidLayoutSubviews() {

        var masterViewController = (self.viewControllers[0] as! UINavigationController).topViewController
        var detailViewController = (self.viewControllers[1] as! UINavigationController).topViewController

        // Adjust the width of the detail view
        var detailViewFrame = detailViewController!.view.frame
        detailViewFrame.origin.x += masterViewController!.view.frame.width
        detailViewFrame.size.width -= masterViewController!.view.frame.width
        detailViewController!.view.frame = detailViewFrame

        detailViewController!.view.setNeedsLayout()
    }

However, when I am in splitViewController?.displayMode == .PrimaryOverlay , and subsequently select a different item from MasterViewController , the DetailViewController frame reverts back to the default frame size and position. I have tried to fix this using the following (although calling viewDidLayoutSubviews() in this way is not recommended):

MasterViewController.swift

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        collapseDetailViewController = false
        splitViewController?.viewDidLayoutSubviews()
    }

Now when I select an item in MasterViewController.swift splitViewController?.viewDidLayoutSubviews() is being called, but I get strange readings when printing the DetailViewController's view.frame.origin.x and view.frame.width at the end of viewDidLayoutSubviews() . And even with these changes to the frame which seem to have been made, the DetailViewController view is still being obscured.

Would appreciate if some help on how to fix the DetailViewController frame so that it is never obscured even when selecting new items in a MasterViewController . I have not had luck implementing any UISplitViewController delegate methods for solving this, and I also tried reloading the SplitViewController with loadView at the end of didSelectRowAtIndexPath but this caused a freeze of the iPad screen.

For anyone else who wanted to know how to get the master and detail view controllers to go side by side without overlay when going onto an iPad, it's really easy! Just add preferredDisplayMode = .AllVisible . No need to use custom SplitViewControllers like MGSplitViewController or alter the frame of the Split View Controller. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM