Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DeviceOrientation Check in AppDelegate

I created a view in the AppDelegate, that I add to the window like this:

[window addSubview:myView];
I wanna be able to check for the device orientation everytime I come back to this view, so I can make some modifications to it. How can I do that in the appDelegate?
like image 514
ludo Avatar asked May 14 '10 02:05

ludo


2 Answers

You could implement one of these methods in the delegate to see when the application rotates:

- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration;
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation;

Or just check the orientation of the UIApplication status bar as needed with:

[[UIApplication sharedApplication] statusBarOrientation];

The device orientation, which may or may not match the interface orientation, is:

[[UIDevice currentDevice] orientation];
like image 180
drawnonward Avatar answered Sep 19 '22 09:09

drawnonward


Here is what I tried first when the app is loading for the first time in the didFinishLauching


[[NSNotificationcenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object: nil];

- (void)orientationChanged:(NSNotification *)notification
{
    [self performSelector:@selector(showScreen) withObject:nil afterDelay:0];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}

-(void)showScreen {

 UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
 if (deviceOrientation == UIDeviceOrientationLandscapeLeft || UIDeviceOrientationLandscapeRight) {

  CGRect screenRect = [[UIScreen mainScreen] bounds];

 }

}

The landscape is detected but the screenRect show width=768 and height=1024 (I'm in an Ipad device).

like image 41
ludo Avatar answered Sep 20 '22 09:09

ludo