Getting Unusual Debug Log With Auto Rotate with Universal App
I have the following code for my universal app but I'm getting this weird log whe开发者_C百科n I run the app. Everything seems to be working fine, however.
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (NSClassFromString(@"UISplitViewController") != nil && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
return YES;
}
else
return NO;
}
In the console:
The view controller <UINavigationController: 0x1468d0> returned NO from -shouldAutorotateToInterfaceOrientation: for all interface orientations. It should support at least one orientation.
The message says everything:
It should support at least one orientation.
In your else
statement, NO
is returned independently of the orientation. If NO
here means "portrait only", do the check and return YES
for portrait:
else
return
(interfaceOrientation == UIInterfaceOrientationPortrait) ?
YES :
NO ;
Or the more succinct (but less fancier) version:
else
return (interfaceOrientation == UIInterfaceOrientationPortrait);
I think that means that your if condition is always false. There should always be a case where you return YES for either portrait or landscape regardless of whether it's an iPad or you have UISplitViewController class. Yours will always return NO for an iPhone for instance. Start with something more like this and then perhaps allow landscape only if blahblah:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (NSClassFromString(@"UISplitViewController") != nil && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
return YES;
}
else
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
I think that your if
condition always fails, so you're probably always returning NO
which means "I don't support any orientation" which is obviously not true... what is the lowest target you want to support?
If your else
is supposed to handle iPhone/iPod, you should return YES
for at least one orientation:
return (interfaceOrientation == UIInterfaceOrientationPortrait);
Or
return inOrientation == UIDeviceOrientationLandscapeLeft
|| inOrientation == UIDeviceOrientationLandscapeRight
|| inOrientation == UIDeviceOrientationPortrait
|| inOrientation == UIDeviceOrientationPortraitUpsideDown;
if you plan to support all orientations.
If you care about supporting iOS versions lower than 3.2 and want to test it on Simulator, you may want change your "iPad check", to sth like this.
- (BOOL)amIAnIPad {
// This "trick" allows compiling for iOS < 3.2 and testing on pre 3.2 simulators
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)
if ([[UIDevice currentDevice] respondsToSelector: @selector(userInterfaceIdiom)])
return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad);
#endif
return NO;
}
More on that trick, can be found on Jeff LaMarche's blog.
精彩评论