UINavigationController setViewControllers自动旋转问题

时间:2013-12-03 18:25:06

标签: ios uinavigationcontroller ios7 autorotate auto-rotation

我在堆栈中呈现一个带有两个视图控制器的UINavigationController,并首先显示最后一个视图控制器。然后点击后退按钮显然会回到第一个视图控制器。

    navCtrl = [[UINavigationController alloc] init];
    ViewController1 *vc1 = [[ViewController1 alloc] init];
    ViewController2* vc2 = [[ViewController2 alloc] init];

    [navCtrl setViewControllers:[NSArray arrayWithObjects:vc1, vc2, nil] animated:NO];
    [self presentViewController:navCtrl animated:YES completion:^{
    }];

问题是当我在横向模式下按导航控制器上的后退按钮时,视图控制器vc1帧不正确。框架为(0,0,320,568)并以横向模式布局。我正在运行iOS 7.按下后退按钮时不会调用自动旋转代码。

然而,如果我使用自然顺序vc1,vc2呈现导航控制器,我看不出任何问题。

编辑:仅供参考,以下是vc1中的-viewWillAppear-viewDidAppear来电:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
    [[UIApplication sharedApplication] setStatusBarHidden:NO
                                            withAnimation:UIStatusBarAnimationNone];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

    NSLog(@"Frame = %@", NSStringFromCGRect(self.view.frame));
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Get status bar height if visible
    if (![UIApplication sharedApplication].statusBarHidden) {
        CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
        CGFloat statusBarHeight = MIN(statusBarFrame.size.height, statusBarFrame.size.width);

        // Set navigation bar frame
        CGRect navBarFrame = self.navigationController.navigationBar.frame;

        [self.navigationController setNavigationBarHidden:YES animated:NO];
        [self.navigationController setNavigationBarHidden:NO animated:NO];
        /*
        navBarFrame.origin.y = statusBarHeight;
        self.navigationController.navigationBar.frame = navBarFrame;
        */
        UIEdgeInsets e = UIEdgeInsetsMake(statusBarHeight + navBarFrame.size.height, 0, navBarFrame.size.height + 12, 0);
        [_tableView setScrollIndicatorInsets:e];
        [_tableView setContentInset:e];
    }

    _tableView.rowHeight = 75;

    self.spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
    spinner.frame = CGRectMake(145, 200, 30, 30);
    [self.view addSubview:spinner];

    [spinner startAnimating];

    [self performSelector:@selector(refreshData) withObject:nil afterDelay:0.f];
}

2 个答案:

答案 0 :(得分:0)

我发现了问题。对于一些我不记得的旧iOS错误解决方法,我在viewDidLoad

中有以下行

[self.navigationController.view setFrame:[self.navigationController.view bounds]];

答案 1 :(得分:0)

您需要在此处查看以下内容:


你说你已经检查过了,但是为了安全起见 - 请检查视图上的自动调整大小。


您的视图控制器是否实现了以下方法?

- (NSUInteger)supportedInterfaceOrientations;

您是否将导航控制器呈现为子视图控制器?执行此操作时,请确保以下方法返回其默认YES

- (BOOL)shouldAutomaticallyForwardRotationMethods;

检查您是否未将子视图控制器添加到UINavigationController视图,因为它不转发外观方法,最好从导航控制器上的视图控制器中处理它。


如果没有上述所有内容,你应该在vc1的viewWillAppear中设置断点并检查其帧,如果帧正确,但navigationContoller.view帧不正确则必须检查自动调整大小掩模。