我如何准备一个UISplitViewController辅助VC来改变displayMode?

在iOS8中,UISplitViewController发生了变化,现在通过splitViewController:willChangeToDisplayMode:通知它的待处理displayMode更改的splitViewController:willChangeToDisplayMode: 我需要更新我的辅助视图控制器的一些方面来响应这个变化。

在这个委托方法中调用辅助VC的方法是很简单的,但是VC还不知道它的新边界是什么。

除了次要VC的边界KVO之外,是否有合理的方式通知VC的边界将会改变? 理想情况下,VC将有viewWillTransitionToSize:withTransitionCoordinator:调用displayMode更改,因为它提供了沿着过渡animation的能力。

所以,现在我只是使用KVO。 我在这里遵循了一些build议。

viewDidLoad

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

然后:

 - (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. } } } 

我在视图的边界上尝试了这一点,但是比帧中的KVO更频繁地发射。

Interesting Posts