2013-02-12 122 views
0

我正在開發一個啓動屏幕並隨後移至登錄屏幕的iPad應用程序。我的應用應該支持所有的方向,並且iOS 4.3及更高版本。要做到這一點,我說的plist中的四個方向,和下面的代碼在應用程序委託:「[[UIApplication sharedApplication] statusBarOrientation];在第一次調用時出現錯誤值」

- (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"); 

} 

我得到的第一個結果錯了,之後的結果是正確的。你能幫我嗎? 請注意,我已經放了一些元素來測試和測試來與相同的結果。 謝謝

回答

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 
    } 
} 

希望這會幫助別人有一天

+0

但從一個視圖導航到另一個時仍然是同樣的問題 – 2013-02-14 10:20:16

相關問題