Reputation: 28905
I've never played around with the iPhone being landscape, but I have a fully working iPhone app that is all assuming the user is viewing the iPhone upright. I'd like to play around with rotating the phone, and in order to do that, I did something very simple:
I added the following code to my View Controller:
-(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
NSLog(@"WILL ROTATE TO INTERFACE ORIENTATION: %@",orientation);
if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight))
tableView.frame = CGRectMake(0, 0, 480, 320);
else
tableView.frame = CGRectMake(0,73,320,390);
}
and I'm getting a BAD_ACCESS error. I don't see the view load at all. So, what's the problem, and how do I properly implement the willRotate method so that when the phone rotates, I can resize all my components?
Upvotes: 0
Views: 4733
Reputation: 73966
It's a problem with your logging code:
NSLog(@"WILL ROTATE TO INTERFACE ORIENTATION: %@",orientation);
The formatting specifier %@
in your logging string is a placeholder for an NSString
pointer. When the app reaches this point, it looks at the orientation variable, attempts to dereference it to get to an NSString
instance, and crashes, because it's not a pointer at all, it's an enum value.
Upvotes: 0
Reputation: 11920
The crash is caused by this line:
NSLog(@"WILL ROTATE TO INTERFACE ORIENTATION: %@",orientation);
orientation
is an enum
value, therefore the format specifier should be %i
not %@
.
The simplest way to support rotating is to implement:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Return YES
for the orientations you wish to support. Do not doing any resizing in this method. Use the views struts and springs to ensure that they resize and reposition themselves correctly for the alternate rotations.
Upvotes: 3