race condition with rotation and split view in iPhone 6 Plus simulator

314 views Asked by At

I have a master/detail view, with a toolbar on the bottom for iPhone and toolbar buttons moved up to the navigation bar for iPad. Here is my code for determining where to put them:

private func isSplit() -> Bool {
    if let svc = self.splitViewController {
        return !svc.collapsed
    }
    else {
        return false
    }
}

func setActionLocation(notification: NSNotification) {
    if self.isSplit() {
        self.toolbarItems = nil
        self.navigationItem.rightBarButtonItems = [self.actionButton, self.deleteButton]
        self.navigationController?.setToolbarHidden(true, animated: false)
    }
    else {
        self.toolbarItems = self.savedToolbar
        self.navigationItem.rightBarButtonItem = nil
        self.navigationController?.setToolbarHidden(false, animated: false)
    }
}

For the iPhone 6 Plus it is split in landscape and collapsed in portrait, so I have this registered as a notification when device orientation changes:

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "setActionLocation:", name: UIDeviceOrientationDidChangeNotification, object: nil)

This all works fine most of the time. However, in the simulator (XCode 6.3.2, iOS Simulator 8.3), if I start in portrait, click to go to detail view, and then quickly (< 1 second) rotate to landscape, isSplit() will still return false and the toolbar ends up in the wrong place. If I wait a little longer before rotating it works properly.

Since I don't have an iPhone 6 Plus, I don't know yet if this occurs in real hardware or if it is an artifact of the simulator. Assuming it is real, is there a way to avoid this?

1

There are 1 answers

0
Ron Stieger On BEST ANSWER

I found the better way to handle this, by removing the rotation notifications and implementing func splitViewController(splitViewController: UISplitViewController, separateSecondaryViewControllerFromPrimaryViewController primaryViewController: UIViewController!) -> UIViewController? in my UISplitViewControllerDelegate. If I was still looking at the master view, I needed to create a detail view controller and tell it to move icons to the top. If I was already looking at a detail view, I could tell that view to move icons to the top. The rotation code handled the second case but not the first, since the detail view controller didn't exist at the time of the rotation, but the split view was still collapsed.

As an aside, this should also be more likely to work with iOS 9 split screen multitasking.