【发布时间】:2011-09-12 03:41:51
【问题描述】:
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;
}
【问题讨论】:
标签: ios uiviewcontroller bundle resourcebundle