2013-02-12 49 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

相关问题