首页 文章

iPhone - 全屏和状态栏的典型问题

提问于
浏览
2

在我的应用程序默认情况下状态栏是可见的 . 我的应用程序支持横向和纵向方向 . 当我想播放视频时,我正在隐藏状态栏,以便我的视频将全屏显示 . 视频完成后,我将状态栏恢复原状 .

我正在使用以下代码来显示视频:

UIWindow* window = [[UIApplication sharedApplication] keyWindow];
[window addSubview:playerView];

(我不能在我的代码中使用视图控制器 . 这是一个限制)

现在问题是:我的应用程序现在处于横向状态,单击按钮,我将隐藏状态栏并开始播放视频 . 播放视频时,我将手机的方向更改为肖像,并允许视频完成 . 视频完成后,设备仍处于纵向模式,播放器视图将被删除,状态栏将再次显示 . 现在我注意到我的纵向视图向上移动了20个像素并且显示状态栏 . 但是当我第一次启动应用程序时,状态栏首先显示在它下面,我的视图显示 .

我应该如何处理这种情况?

在视图控制器中简单使用以下代码 .

- (void)viewDidLoad {
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [self performSelector:@selector(showStatusAgain) withObject:nil afterDelay:6.0];
    [super viewDidLoad];
}

-(void)showStatusAgain
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

当您使用上面的代码运行应用程序时,它将以纵向方式启动 . 现在将其旋转为横向 . 你可以注意到这个问题 .

2 回答

  • 6

    我遇到了类似的问题,我用UINavigationController上的类别解决了这个问题 .

    UINavigationController LayoutCorrecting.h:

    @interface UINavigationController (LayoutCorrecting)
    
    - (void)recalculateNavigationBarFrameRelativeToStatusBar;
    
    @end
    

    UINavigationController LayoutCorrecting.m:

    #import "UINavigationBar+LayoutCorrecting.h"
    
    #import "UIViewAdditions.h"
    
    @implementation UINavigationController (LayoutCorrecting)
    
    - (void)recalculateNavigationBarFrameRelativeToStatusBar {
        CGRect sbf = [[UIApplication sharedApplication] statusBarFrame];
    
        if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
            self.navigationBar.top = sbf.size.width;
    
        else
            self.navigationBar.top = sbf.size.height;
    }
    
    @end
    

    如果你通过在UIViewController上声明它来调整这个类别,并确保它调整controller.view.frame而不是navigationBar.top,我想你会很高兴 . 只需确保在电影结束播放后在视图控制器上调用类别方法,并且您已经显示了状态栏 .

    只是为了避免任何混淆,尽管你如何握住设备,它的坐标系总是像纵向一样工作 . 这与由UIViewController管理的子视图(UIView实例)形成对比,后者受益于自动坐标转换 .

    P.S . :由于您将不得不在窗口上下文中调整视图的框架,因此该功能也可能有所帮助 .

    CGRect CGRectOffsetRectForOrientation(CGRect rect, CGFloat dx, CGFloat dy, UIInterfaceOrientation orientation) {
        CGRect newRect = rect;
    
        switch (orientation) {
            case UIInterfaceOrientationPortrait:
                newRect.origin.x += dx;
                newRect.origin.y += dy;
                break;
    
            case UIInterfaceOrientationPortraitUpsideDown:
                newRect.origin.x -= dx;
                newRect.origin.y -= dy;
                break;
    
            case UIInterfaceOrientationLandscapeLeft:
                newRect.origin.y -= dx;
                newRect.origin.x += dy;
                break;
    
            case UIInterfaceOrientationLandscapeRight:
                newRect.origin.y += dx;
                newRect.origin.x -= dy;
                break;
    
            default:
                break;
        }
    
        return newRect;
    }
    
  • 0

    在显示时尝试更新状态栏样式:

    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefualt];
    

相关问题