Why won't my views rotate?
I have a Tab Bar application for iPad, created using the basic Tab Bar template. I have added some custom view controllers (one for each tab, each with a corresponding NIB) and also some extra view controllers with NIBs to be used as modal views. Everything works great until I rotate the device.
My app only supports portrait orientation, so I had this in all my view controllers:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIDeviceOrientationLandscapeLeft) &&
(interfaceOrientation != UIDeviceOrientationLandscapeRight);
}
However, the app would not rotate in the simulator or the device when turned upside down. I double and triple checked that all my view controllers had the above code.
I went through all my NIBs and checked that they all have "Rotate Subviews" ticked. I haven't changed any of the NIB settings from the defaults anyway, apart from the basic things needed to get them showing in the tab views.
I tried changing the code in all my view controllers to this:
- (BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
It made no difference. I have made absolutely sure that the same method is being used in all the view controllers. I don't know what else I can do. I can see no reason why it shouldn't rotate to the upside down view.
开发者_Python百科Any help with this would be much appreciated.
Got it! One of my View Controllers was not hooked up to the relevant tab in IB. As I hadn't added the images or written the code for that View Controller yet, I didn't notice that it wasn't associated in IB. I had done the shouldAutorotateToInterfaceOrientation method, but it seems that didn't take effect until the connection was made in IB.
Thanks very much for suggestions on this. That's a highly frustrating problem now dealt with!
Also, this is Apple's very helpful guide: http://developer.apple.com/library/ios/#qa/qa1688/_index.html
In my case - I forgot to call self = [super initWithNibName ....]!
Does "all your view controllers" include the Tab Bar Controller?
In tab bar apps that is the only view controller who's shouldAutoRotateToInterfaceOrientation
is called and evaluated at all.
The first snippet you have is logically incorrect:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
return (orientation != UIDeviceOrientationLandscapeLeft) &&
(orientation != UIDeviceOrientationLandscapeRight);
}
Here, orientation
is an instance of UIInterfaceOrientation
whereas UIDeviceOrientationLandscapeLeft
is an instance of UIDeviceOrientation
. The two are not the same type and so should not be compared.
Instead, you should use the UIInterfaceOrientation
options:
typedef enum {
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeLeft,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeRight
} UIInterfaceOrientation;
Change the method to
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
return (orientation == UIInterfaceOrientationLandscapeLeft ||
orientation == UIInterfaceOrientationLandscapeRight);
}
(the code seems to me more readable when put in the affirmative rather than negative)
精彩评论