Reputation: 6445
In my root view controller's viewDidLoad, i added an observer for orientation detection:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willRotateToInterfaceOrientation:duration:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
Added the method like:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
NSLog(@"%f,%f",self.view.frame.size.width,self.view.frame.size.height);
}
I was expecting the result 768,1004 for the portrait and 1004,768 for the landscape. But my result is 768,1004 for the both. sometimes it will be 748,1024 for the portrait. I dont know why this strange behaviour. If you know the solution please help me.
EDIT This same code works in my another project and i couldnt find any difference between them
Upvotes: 0
Views: 198
Reputation: 104082
First off, you don't need to add an observer for that notification, view controllers are sent this notification by default. So, remove those first two lines, and it should work properly. And since this is the "will" method, properly means you get the size of the view before the rotation.
However, the method, willAnimateRotationToInterfaceOrientation:duration:, does give you the dimensions of the view after the rotation if that's what you want (even though it's a "will" method).
After Edit
For some reason these methods work correctly for a controller embedded in a navigation controller, but not for a single controller, or a modal controller. For those situations, switching from bounds from frame, fixed the problem.
Upvotes: 0
Reputation: 6918
Add these two methods:
- (BOOL)shouldAutorotate
{
return TRUE;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
And do all modifications here:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration
{
NSLog(@"%f,%f",self.view.frame.size.width,self.view.frame.size.height);
}
Upvotes: 1
Reputation: 89509
[If you look at Apple's documentation for willRotateToInterfaceOrientation
, it says]:
Sent to the view controller just before the user interface begins rotating.
This means the frame width & height should be for the orientation before rotating.
Perhaps if you try to get the frame coordinates after the rotation is done, say, during didRotateFromInterfaceOrientation
, you might see something more like what you were hoping to accomplish.
Upvotes: 0