How can I prepare a UISplitViewController secondary VC for a change to displayMode?

390 views Asked by At

In iOS8, UISplitViewController changed, and now notifies its delegate of a pending displayMode change via splitViewController:willChangeToDisplayMode:. I need to update some aspects of my secondary view controller in response to this change.

It's simple enough to call methods on the secondary VC during this delegate method, but the VC doesn't yet know what its new bounds will be.

Is there a reasonable way to be notified that the bounds of the VC will change, apart from KVO on the bounds of the secondary VC? Ideally, the VC would have viewWillTransitionToSize:withTransitionCoordinator: called for the displayMode change, since that provides for the ability to animate alongside the transition.

1

There are 1 answers

0
Jeff V On

So, for now I'm just using KVO. I followed some of the advice here.

In viewDidLoad:

[self.view addObserver:self
            forKeyPath:NSStringFromSelector(@selector(frame))
               options:(NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew)
               context:nil];

Then:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([object isKindOfClass:[UIScrollView class]] && [keyPath isEqualToString:NSStringFromSelector(@selector(frame))]) {
        CGRect newFrame = [change[@"new"] CGRectValue];
        CGRect oldFrame = [change[@"old"] CGRectValue];

        if ((newFrame.size.width == oldFrame.size.width) || (newFrame.size.height == oldFrame.size.height)) {
            // If one dimension remained constant, we assume this is a displayMode change instead of a rotation

            // Make whatever changes are required here, with access to new and old frame sizes.
        }
    }
}

I tried this on the bounds of the view, but that fired a lot more frequently than the KVO on the frame.