在应用程序启动时了解iPad的界面方向

时间:2012-02-28 15:46:43

标签: ipad orientation

我试图弄清楚我的iPad在平台上启动时的方向。

我总是说它是以肖像模式开始的,但事实并非如此。

我使用了采样代码here来检测启动时的方向。

当我的设备面朝上在平面上时,它声称它处于纵向模式。但是状态栏在横向模式下是可视的。

有解决方法吗?

我正在使用iOS 5和Xcode 4.3。

编辑:更多详情

这是我的更新方向方法:

- (void)updateOrientation {

    UIInterfaceOrientation iOrientation = self.interfaceOrientation;//[[UIApplication sharedApplication] statusBarOrientation];
    UIDeviceOrientation dOrientation = [UIDevice currentDevice].orientation;

    bool landscape;

    if (dOrientation == UIDeviceOrientationUnknown || dOrientation == UIDeviceOrientationFaceUp || dOrientation == UIDeviceOrientationFaceDown) {
        // If the device is laying down, use the UIInterfaceOrientation based on the status bar.
        landscape = UIInterfaceOrientationIsLandscape(iOrientation);
    } else {
        // If the device is not laying down, use UIDeviceOrientation.
        landscape = UIDeviceOrientationIsLandscape(dOrientation);

        // There's a bug in iOS!!!! http://openradar.appspot.com/7216046
        // So values needs to be reversed for landscape!
        if (dOrientation == UIDeviceOrientationLandscapeLeft) iOrientation = UIInterfaceOrientationLandscapeRight;
        else if (dOrientation == UIDeviceOrientationLandscapeRight) iOrientation = UIInterfaceOrientationLandscapeLeft;

        else if (dOrientation == UIDeviceOrientationPortrait) iOrientation = UIInterfaceOrientationPortrait;
        else if (dOrientation == UIDeviceOrientationPortraitUpsideDown) iOrientation = UIInterfaceOrientationPortraitUpsideDown;
    }

    whiteView.hidden = NO;
    splashScreen.hidden = NO;
    if (landscape) {
        splashScreen.image = [UIImage imageNamed:@"Default-Landscape~ipad"];
    } else {
        splashScreen.image = [UIImage imageNamed:@"Default-Portrait~ipad"];
    }

    splashScreen.contentMode = UIViewContentModeScaleToFill;
    [self.view bringSubviewToFront:whiteView];
    [self.view bringSubviewToFront:splashScreen];

    // Set the status bar to the right spot just in case
    [[UIApplication sharedApplication] setStatusBarOrientation:iOrientation];
}

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

    if (splashScreen){
        [self updateOrientation];
        [UIView animateWithDuration:0.5 delay:1 options:UIViewAnimationOptionCurveEaseInOut 
                         animations:^{
                             splashScreen.alpha = 0;
                         } 
                         completion:^(BOOL success){
                             [splashScreen removeFromSuperview];
                             splashScreen = nil;
                         }];

        [UIView animateWithDuration:0.5 delay:1.5 options:UIViewAnimationOptionCurveEaseInOut 
                         animations:^{
                             whiteView.alpha = 0;
                         } 
                         completion:^(BOOL success){
                             [whiteView removeFromSuperview];
                             whiteView = nil;
                         }];
}

当我在横向模式下在平面上启动应用程序时,问题显而易见我看到为spashscreen加载了错误的图像。

3 个答案:

答案 0 :(得分:5)

使用[[UIApplication sharedApplication] statusBarOrientation]代替[UIDevice currentDevice] orientation]

设备方向和用户界面方向(以及some elaboration can be found on this related question)之间存在差异。许多应用程序可能具有用户界面,即使设备以另一种方式指向,也只能以某种方式工作。

答案 1 :(得分:2)

我遇到了同样的问题,我没有找到任何好的解决方案。

似乎[[UIApplication sharedApplication] statusBarOrientation]总是在application:didFinishLaunchingWithOptions:完成之前返回1,并且只有当您的设备正面朝上时(也可能面朝下,我还没试过)

我终于做到了:

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

    // For the cases where device is not face up.
    if(UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))
        [self doTheJobInPortrait];
    else
        [self doTheJobInLandscape];

    // For the cases where device is face up
    [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(hackForFaceUpLandscapeDevices) userInfo:nil repeats:NO];
}

-(void)hackForFaceUpLandscapeDevices {
    if(UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))
        [self doTheJobInPortrait];
    else
        [self doTheJobInLandscape];
}

当然,这项工作将完成两次,面朝上的景观模式会有一个短暂的时刻,你的界面将是纵向...

如果有人找到更好的解决方案,我很乐意知道。

答案 2 :(得分:0)

这就是为什么在启动时经常使用“空白视图”,只是初始化您的应用程序。当第一个视图被加盖时,您可以轻松检查设备方向是否正确。

在此之后加载特定于设备方向的视图。所以你有:

一个ViewController 一个空白视图 内容的两个视图(一个风景,一个肖像)

我想,e。 G。 Safari Mobile就是这样做的。