简体   繁体   中英

How to detect the orientation changing ipad

Im creating an iPad application that should be supported both landscape and portrait. I have set root view controller in AppDelegate like this. This view is my Splash screen view.

Inside

`

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

  if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft)
{
    //Do what you want in Landscape Left
    viewController1=[[SplashLandscapeViewController alloc] initWithNibName:@"SplashLandscapeViewController" bundle:nil];
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
    //Do what you want in Landscape Right
    viewController1=[[SplashLandscapeViewController alloc] initWithNibName:@"SplashLandscapeViewController" bundle:nil];
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait)
{
    //Do what you want in Portrait
    viewController1=[[SplashLandscapeViewController alloc] initWithNibName:@"SplashViewController" bundle:nil];
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
    //Do what you want in Portrait Upside Down
    viewController1=[[SplashLandscapeViewController alloc] initWithNibName:@"SplashViewController" bundle:nil];
}





self.window.rootViewController = viewController1;
[self.window makeKeyAndVisible];

return YES;

}

And this is working fine. And this viewcontroller1 is dissmiss after 4,5 seconds and load another viewcontroller. My problem is while that viewcontroller1 showing on the screen, if user turn the ipad how can I recogniz it and load my landscape .xib after removing the current portrait xib.

Please help me. Thank you

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeOrientation) name:UIDeviceOrientationDidChangeNotification object:nil];

add this method into the app view controller for getting notification.

-(void)changeOrientation {
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
        [[NSBundle mainBundle] loadNibNamed:@"Portrait" owner:self options:nil];
    }
    else {
        [[NSBundle mainBundle] loadNibNamed:@"Landscape" owner:self options:nil];
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM