setStatusBarOrientation

setStatusBarOrientation

我有一个导航控制器应用程序。首先,我按FirstViewController(支持纵向方向),然后按SecondViewController(支持所有方向)。当我处于SecondViewController的横向模式并按返回按钮时,FirstViewController会以横向模式出现。这就是为什么我手动旋转导航视图的原因,但是当我想将setStatusBarOrientation设置为纵向(第一个视图控制器应该仅在纵向模式下出现)时,视图控制器的方向仍然是横向的,即使将设备旋转到纵向模式,方向停留景观
这是我的FirstViewController代码:

- (void)viewWillAppear:(BOOL)animated
{
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
    {
        if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
            prevInterfaceOrientation = UIInterfaceOrientationLandscapeLeft;
            self.navigationController.view.transform = CGAffineTransformIdentity;
            self.navigationController.view.transform =
            CGAffineTransformMakeRotation(degreesToRadians(90));
        }
        else if (self.interfaceOrientation ==
                 UIInterfaceOrientationLandscapeRight) {
            prevInterfaceOrientation = UIInterfaceOrientationLandscapeRight;
            self.navigationController.view.transform = CGAffineTransformIdentity;
            self.navigationController.view.transform =
            CGAffineTransformMakeRotation(degreesToRadians(-90));
        }
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
        [self.tableViewDetail reloadData];
    }


}

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
     return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
    {
        if (prevInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
            self.navigationController.view.transform = CGAffineTransformIdentity;
        }
        else if (prevInterfaceOrientation ==
                 UIInterfaceOrientationLandscapeRight) {
            self.navigationController.view.transform = CGAffineTransformIdentity;
        }
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
        [self.tableViewDetail reloadData];
    }
}


我什至尝试使用:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    if (UIInterfaceOrientationIsLandscape(prevInterfaceOrientation))
    {
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
    }
}


但是当我旋转到纵向时,self.interfaceOrientation仍然保持横向。
但是我真的需要手动将视图旋转到纵向模式,以使用户看到FirstViewController仅支持纵向。
我可以选择将SecondViewController的视图放在MainWindow(如模态窗口)上,但是我不喜欢这个想法,因为如果苹果有setStatusBarOrientation方法,在我看来,它必须正确解决这个问题。

最佳答案

我会摆脱转换,并使用

      [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:animated];


结合强制重画视图堆栈可以完成此操作。这可以通过将以下内容添加到viewDidAppear到第一个控制器中来完成(它在viewWillApear中不起作用)。

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

       UIWindow *window = [[UIApplication sharedApplication] keyWindow];
       if ([window.subviews count] > 0) {
           UIView *view = [window.subviews objectAtIndex:0];
           [view removeFromSuperview];
           [window insertSubview:view atIndex:0];
       }
       else {
           DLog(@"NO view to force rotate?");
       }


不幸的是,执行此操作时过渡动画不是很干净,因此我建议拍摄人像屏幕的快照,将其覆盖在视图上,然后使用单独的动画将其淡出。

关于iphone - setStatusBarOrientation问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10796210/

10-09 18:31