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



在平板电脑上启动时,我正试图弄清楚iPad的方向。

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

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

碰巧,当我的设备正面朝上放在平面上时,它声称自己处于人像模式。但是,状态栏在视觉上处于横向模式。

有办法绕过这个吗?

我使用的是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加载了错误的图像。

使用[[UIApplication sharedApplication] statusBarOrientation]而不是[UIDevice currentDevice] orientation]

设备定向和用户界面定向之间存在差异(在这个相关问题上可以找到一些详细说明)。许多应用程序的用户界面可能只设计为以特定方式工作,即使设备指向另一个方向。

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

似乎[[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];
}

当然,这项工作将完成两次,在正视街景模式下,会有一小段时间,你的界面将是人像。。。

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

这就是为什么在启动时经常使用"空白视图",它只是初始化你的应用程序。当第一个视图变老时,您可以很容易地检查设备的正确方向。

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

一个ViewController一个空白视图带内容的两个视图(一个横向,一个纵向)

我认为,例如Safari Mobile就是这样做的。

最新更新