Reputation: 1479
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?
Upvotes: 5
Views: 8757
Reputation: 1479
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).
Upvotes: 1
Reputation: 6769
In Apples examples you get notified via the delegate method:
- (void)orientationChanged:(NSNotification *)notification
{
// We must add a delay here, otherwise we'll swap in the new view
// too quickly and we'll get an animation glitch
NSLog(@"orientationChanged");
[self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}
And then to display a portrait screen:
- (void)updateLandscapeView
{
PortraitView *portraitView = [[PortraitView alloc] init];
portraitView.delegate = self;
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
{
[self presentModalViewController: portraitView animated:YES];
isShowingLandscapeView = YES;
}
else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
{
[self dismissModalViewControllerAnimated:YES];
isShowingLandscapeView = NO;
}
[portraitView release];
}
Of course you have to design the PortraitView as a delegate class for this to work as intended.
Not the only way but I find it works well and its in Apples examples. I wouldn't do it in the Appdelegate though but rather your uiview, I don't know ur design though.
Upvotes: 0
Reputation: 53669
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];
Upvotes: 11