“[[UIApplication sharedApplication] statusBarOrientation];在第一次调用中给出错误的值”

时间:2013-02-12 17:08:54

标签: ipad orientation

我正在开发一个以启动画面开始的iPad应用程序,然后移动到登录屏幕。我的应用程序应支持所有方向,iOS 4.3及更高版本。为此,我在plist中添加了四个方向,并在app delegate中添加了以下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    [self.window makeKeyAndVisible]; 
    // Override point for customization after application launch
    SplashViewController *aController = [[SplashViewController alloc] initWithNibName:@"SplashView" bundle:nil];
    self.mainViewController = aController;
    [aController release];

    mainViewController.view.frame = [UIScreen mainScreen].bounds;// no effect
    [window setFrame:[[UIScreen mainScreen] bounds]]; //no effect
    //[window addSubview:[mainViewController view]];
    [window setRootViewController:mainViewController];

    return YES;
}

- (NSUInteger)application:(UIApplication *)application     supportedInterfaceOrientationsForWindow:(UIWindow *)window{

    return UIInterfaceOrientationMaskAll;
}

在启动画面

- (void) loadView {
    [super loadView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return YES;
}
- (BOOL)shouldAutorotate{
    return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}
- (void) orientationChanged
{
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            NSLog(@"Portrait");
    else
            NSLog(@"Landscape");

}

在这里,我获得了正确的方向,但是在旋转时,我获得了倒置的结果,我获得了用于肖像的风景和用于风景的肖像。我试图像这样转换代码:

- (void) orientationChanged
{
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            NSLog(@"Landscape");
    else
            NSLog(@"Portrait");

}

我得到第一个结果错误,之后结果是正确的。你能帮我吗? 请注意,我已经尝试了一些测试,测试结果相同。 谢谢

1 个答案:

答案 0 :(得分:0)

经过两天的搜索和测试,我设法解决了这个问题,论坛中没有一个答案是完整的,这就是我解决的问题:

//this for the orientation
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self orientationChanged];
}

// here is the tricky thing, when startup you should have a special function for the orientation other than the orientationchanged method, and must be called here in viewDidAppear, otherwise it won't work
-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self StartUpOrientation];
}
#pragma mark -
#pragma mark Orientation Methods

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

- (BOOL)shouldAutorotate {
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;

}

- (void) orientationChanged {
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
    if (interfaceOrientation != UIInterfaceOrientationLandscapeLeft && interfaceOrientation != UIInterfaceOrientationLandscapeRight) {
        [self.view setBounds:CGRectMake(0, 0, 1024, 748)];
        // your code here
    }
    else {
        [self.view setBounds:CGRectMake(0, 0,768,1004)];
        //your code here
    }
}

- (void) StartUpOrientation {
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
if (interfaceOrientation != UIInterfaceOrientationLandscapeLeft && interfaceOrientation != UIInterfaceOrientationLandscapeRight) {
        [self.view setBounds:CGRectMake(0, 0,768,1004)];
        // your code here
    }
else {
        [self.view setBounds:CGRectMake(0, 0, 1024, 748)];
        // your code here
    }
}

希望有一天能帮助某人