显示子视图控制器时,父视图控制器不会旋转

Jac*_*ies 4 iphone objective-c uiviewcontroller ipad ios

我有一个父视图控制器,支持所有四个方向.它提供了一个子视图控制器,如果在显示子视图控制器时旋转设备,然后关闭子视图,则父视图控制器不能正确旋转.这是我在父视图控制器中用于旋转的代码:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {

    newestIssueCoverImageButton.frame = CGRectMake(40, 20, 688, 865);
    shadow.frame = CGRectMake(40, 20, 688, 865);
    recordView.frame = CGRectMake(0, 44, 768, 916);
    classifiedsWebView.frame = CGRectMake(0, 44, 768, 916);

} else {

    newestIssueCoverImageButton.frame = CGRectMake(269, 20, 486, 613);
    shadow.frame = CGRectMake(269, 20, 486, 613);
    recordView.frame = CGRectMake(0, 44, 1024, 660);
    classifiedsWebView.frame = CGRectMake(0, 44, 1024, 660);

}
Run Code Online (Sandbox Code Playgroud)

虽然这段代码没有被使用,但是我只是旋转了设备.如何调用此代码并确保在用户当前正在查看子项时正确旋转父视图控制器?谢谢你的帮助.

小智 9

来自Apple的文档:

如果在发生方向更改时视图控制器不可见,则永远不会调用旋转方法.但是,当视图变为可见时,将调用viewWillLayoutSubviews方法.您对此方法的实现可以调用statusBarOrientation方法来确定设备方向.

我添加了一个BOOL来跟踪它不是顶级viewController,然后:

- (void)viewWillLayoutSubviews {

    if (notTopController) {

        [self willRotateToInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation] duration:0];
        [self willAnimateRotationToInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation] duration:0];

       notTopController = NO;

}
Run Code Online (Sandbox Code Playgroud)

到目前为止对我有用.