在popViewControllerAnimated上没有调用willAnimateRotationToInterfaceOrientation

tcu*_*rdt 11 iphone orientation ipad

在MainViewController中,我必须在方向更改时调整一些图像.简单 - 只需将代码添加到willAnimateRotationToInterfaceOrientation: callback

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
  NSLog(@"orientation: %@", name(orientation));
  ..
Run Code Online (Sandbox Code Playgroud)

按预期工作.在Portrait中,我将另一个方向感知UIViewController推送到UINavigationController上.我转向风景,一切都很好.

然后我通过调用返回MainViewController

[self.navigationController popViewControllerAnimated:YES];
Run Code Online (Sandbox Code Playgroud)

当MainViewController的子视图根据autoresize蒙版调整为Landscape时,willAnimateRotationToInterfaceOrientation:不会被调用!

我本来期望willAnimateRotationToInterfaceOrientation:要么在不在导航控制器堆栈之上时调用,要么在返回到堆栈顶部时至少被调用.

我假设我可以手动调用willAnimateRotationToInterfaceOrientation:在pop之前但感觉不对.

我错过了什么?

Sco*_*oth 36

这是我所看到的预期行为.如果UIVC不在堆栈顶部,则不应调用willAnimateRotationToInterfaceOrientation,因为此时没有动画旋转.我在我正在处理的应用程序中处理此问题的方式与上面的海报类似.任何支持所有方向的UIVC都会获得一种新方法

- (void) updateLayoutForNewOrientation: (UIInterfaceOrientation) orientation;

从两个地方调用此方法:


-(void) viewWillAppear: (BOOL) animated {
  [super viewWillAppear: animated];
  [self updateLayoutForNewOrientation: self.interfaceOrientation];
}

-(void) willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation duration: (NSTimeInterval) duration { [self updateLayoutForNewOrientation: interfaceOrientation]; }

新方法很简单:


- (void) updateLayoutForNewOrientation: (UIInterfaceOrientation) orientation {
    if (UIInterfaceOrientationIsLandscape(orientation)) {
        // Do some stuff
    } else {
        // Do some other stuff
    }
}

此外,如果您担心代码在实际不需要时运行,您可以通过viewDidDisappear中设置的实例变量将新UIVC推送到堆栈时跟踪设备的方向并查阅它以确定是否需要去做"做的事情"