3

I have a master-detail application (I created it using the Xcode template, and then I modified it a bit), and I'm trying to set the preferredDisplayMode property of UISplitViewController to obtain this behavior:

UISplitViewControllerDisplayMode.PrimaryOverlay: The primary view controller is layered on top of the secondary view controller, leaving the secondary view controller partially visible.

So the master view controller should initially be on top of the detail view controller, and it should be possible to dismiss it. I change this property in application:didFinishLaunchingWithOptions:, that's the full code:

 
// Inside application:didFinishLaunchingWithOptions:
// Override point for customization after application launch.
let rootViewController = window!.rootViewController as! UINavigationController
// The root view controller is a navigation controller that contains the split view controller
let splitViewController = rootViewController.viewControllers[0] as! UISplitViewController

let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem() splitViewController.delegate = self

splitViewController.preferredPrimaryColumnWidthFraction = 0.4 splitViewController.maximumPrimaryColumnWidth = 600 splitViewController.preferredDisplayMode = .PrimaryOverlay

return true

I have two problems: first, that's not the behavior that I obtain. The master view controller is hidden when the application launches, and if I click on the left bar button item to show the master, it rapidly appears and then disappears again. If I click it another time it appears without disappearing.
Second, I get a warning in the console:

2015-06-30 12:06:26.613 Presidents[29557:857547] Unbalanced calls to begin/end appearance transitions for <UINavigationController: 0x7b8be610>.

But I have no transitions in my code.

PS: It's from the book "Beginning Phone Development with Swift" by D. Mark, J. Nutting, K. Topley, F. Olsson, J. LaMarche, chapter 11.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187

1 Answers1

0

I got this working in my iPad app. In the Master View Controller:

override func viewDidLoad() {
    super.viewDidLoad()

    splitViewController?.delegate = self

    let rect: CGRect = UIScreen.mainScreen().bounds
    if rect.height > rect.width {
        // am in portrait: trick to force the master view to open
        self.splitViewController?.preferredDisplayMode = .PrimaryOverlay
    }

Then later:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    self.splitViewController?.preferredDisplayMode = .Automatic

Now trying to find out how to do it with an iPhone app... EDIT: ah, see this previous answer

Community
  • 1
  • 1
David H
  • 40,852
  • 12
  • 92
  • 138