如何准备 UISplitViewController 辅助 VC 以更改 displayMode? [英] How can I prepare a UISplitViewController secondary VC for a change to displayMode?

查看:22
本文介绍了如何准备 UISplitViewController 辅助 VC 以更改 displayMode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iOS8 中,UISplitViewController 发生了变化,现在通过 splitViewController:willChangeToDisplayMode: 通知它的委托有待处理的 displayMode 变化.我需要更新辅助视图控制器的某些方面以响应此更改.

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.

在此委托方法期间调用辅助 VC 上的方法很简单,但 VC 尚不知道它的新边界是什么.

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.

除了辅助VC的边界上的KVO之外,是否有合理的方式来通知VC的边界会发生变化?理想情况下,VC 应该让 viewWillTransitionToSize:withTransitionCoordinator: 调用 displayMode 更改,因为这提供了与过渡一起动画的能力.

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.

推荐答案

所以,现在我只使用 KVO.我遵循了此处的一些建议.

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

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 触发的频率要高得多.

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

这篇关于如何准备 UISplitViewController 辅助 VC 以更改 displayMode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆