Xcode(4)突出显示了UISupportedInterfaceOrientations设置,但无论我如何设置它们,它似乎都不会影响基本应用程序的功能。 Xcode模板插入注释掉的实现,以编程方式报告支持:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
根据我的经验,此代码与应用程序的捆绑设置无关。因此,我编写了以下代码,以根据应用程序设置自动报告对方向的支持。
为什么这不是默认实现?我错过了什么,这段代码不是必需的吗? (任何人都可以建议改进吗?)我可能会把它变成UIViewController的类别
/**
* This implementation coordinates the app's orientation support with the app
* bundle settings, simplifying the configuration of the app's support for its
* different orientations.
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
static NSString *_orientationFlagNames[] = {
Nil, // UIDeviceOrientationUnknown
@"UIInterfaceOrientationPortrait", // UIDeviceOrientationPortrait,
@"UIInterfaceOrientationPortraitUpsideDown",// UIDeviceOrientationPortraitUpsideDown,
@"UIInterfaceOrientationLandscapeRight", // UIDeviceOrientationLandscapeLeft [sic]
@"UIInterfaceOrientationLandscapeLeft" // UIDeviceOrientationLandscapeRight [sic]
// UIDeviceOrientationFaceUp
// UIDeviceOrientationFaceDown
};
NSArray *supportedOrientation = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"];
BOOL shouldRotate = (interfaceOrientation < sizeof(_orientationFlagNames)/sizeof(NSString*)
&& [supportedOrientation containsObject:_orientationFlagNames[interfaceOrientation]]);
return shouldRotate;
}
答案 0 :(得分:1)
我认为这里有一个区别。 info.plist
值表示应用支持的方向,而shouldAutorotateToInterfaceOrientation:
表示特定视图可用的方向。
如果您查看here
,则会明确提到info.plist
值可帮助iOS选择启动应用程序的初始方向。