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

吃可爱长大的小学妹 提交于 2019-12-11 10:03:22

问题


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:


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.



来源:https://stackoverflow.com/questions/25696281/how-can-i-prepare-a-uisplitviewcontroller-secondary-vc-for-a-change-to-displaymo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!