这是真正的答案。当应用程序启动时,它的方向是未知的。它使用shouldAutorotateToInterfaceOrientation和supportedInterfaceOrientations来决定选择哪个方向。
看我如何在iPhone 5.0模拟器中启动一个示例应用程序,并使用下面的代码和“支持的界面方向”旋转它,包括所有4个可能的方向:
20:44:08.218 RotationTestApp Supported orientation: Portrait
20:44:08.222 RotationTestApp Supported orientation: Portrait (upside-down)
20:44:08.225 RotationTestApp Supported orientation: Landscape (home button on the right)
20:44:08.225 RotationTestApp Supported orientation: Landscape (home button on the left)
20:44:08.226 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:08.237 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:08.239 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:08.240 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:09.817 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeLeft)
20:44:09.833 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeLeft)
20:44:11.030 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)
20:44:11.040 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)
20:44:12.599 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeRight)
20:44:12.609 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeRight)
20:44:13.301 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)
我看过很多代码片段,但它们都不够通用(iPad和iPhone,iOS 5.0+)。
与其试错,不如将以下内容放在您的根视图控制器中:
#define ToNSString_BEGIN(T) \
NSString* T##ToNSString(T valueParameter) { \
switch (valueParameter) {
#define ToNSString_VALUE(value) \
case value: return @#value
#define ToNSString_END(T) \
} \
return @"(unknown)"; \
}
ToNSString_BEGIN(UIInterfaceOrientation);
ToNSString_VALUE(UIInterfaceOrientationPortrait);
ToNSString_VALUE(UIInterfaceOrientationPortraitUpsideDown);
ToNSString_VALUE(UIInterfaceOrientationLandscapeLeft);
ToNSString_VALUE(UIInterfaceOrientationLandscapeRight);
ToNSString_END (UIInterfaceOrientation);
ToNSString_BEGIN(UIDeviceOrientation);
ToNSString_VALUE(UIDeviceOrientationUnknown);
ToNSString_VALUE(UIDeviceOrientationPortrait);
ToNSString_VALUE(UIDeviceOrientationPortraitUpsideDown);
ToNSString_VALUE(UIDeviceOrientationLandscapeLeft);
ToNSString_VALUE(UIDeviceOrientationLandscapeRight);
ToNSString_VALUE(UIDeviceOrientationFaceUp);
ToNSString_VALUE(UIDeviceOrientationFaceDown);
ToNSString_END (UIDeviceOrientation);
- (BOOL)allowAutoRotate:(UIInterfaceOrientation)interfaceOrientation
{
NSUInteger interfaceOrientationAsMask = (1<<interfaceOrientation);
return interfaceOrientationAsMask & [self supportedInterfaceOrientations];
}
- (NSUInteger)supportedInterfaceOrientations
{
static NSUInteger orientationsResult;
if (!orientationsResult) {
NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];
for (id orientationString in supportedOrientations) {
if ([orientationString isEqualToString:@"UIInterfaceOrientationPortrait"]) {
orientationsResult |= UIInterfaceOrientationMaskPortrait;
NSLog(@"Supported orientation: Portrait");
} else if ([orientationString isEqualToString:@"UIInterfaceOrientationPortraitUpsideDown"]) {
orientationsResult |= UIInterfaceOrientationMaskPortraitUpsideDown;
NSLog(@"Supported orientation: Portrait (upside-down)");
} else if ([orientationString isEqualToString:@"UIInterfaceOrientationLandscapeRight"]) {
orientationsResult |= UIInterfaceOrientationMaskLandscapeRight;
NSLog(@"Supported orientation: Landscape (home button on the left)");
} else if ([orientationString isEqualToString:@"UIInterfaceOrientationLandscapeLeft"]) {
orientationsResult |= UIInterfaceOrientationMaskLandscapeLeft;
NSLog(@"Supported orientation: Landscape (home button on the right)");
} else {
NSLog(@"Unrecognized orientation '%@' in mainBundle plist, key UISupportedInterfaceOrientations", orientationString);
}
}
}
return orientationsResult;
}
- (BOOL)shouldAutorotate
{
UIDeviceOrientation interfaceOrientationFromDevice = [UIDevice currentDevice].orientation;
BOOL result = [self allowAutoRotate:interfaceOrientationFromDevice];
NSString *currentDeviceOrientation = UIDeviceOrientationToNSString(interfaceOrientationFromDevice);
NSLog(@"shouldAutorotate: %s (current orientation %@)", result ? "YES" : "NO", currentDeviceOrientation);
return result;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
NSString* orientationString;
UIDeviceOrientation interfaceOrientationFromDevice = [UIDevice currentDevice].orientation;
if ((int)interfaceOrientation != (int)interfaceOrientationFromDevice) {
orientationString = [NSString stringWithFormat:@"current device orientation: %@, interface orientation wants: %@",
UIDeviceOrientationToNSString(interfaceOrientationFromDevice),
UIInterfaceOrientationToNSString(interfaceOrientation)
];
} else {
orientationString = [NSString stringWithFormat:@"device orientation: %@", UIDeviceOrientationToNSString(interfaceOrientationFromDevice)
];
}
BOOL result = [self allowAutoRotate:interfaceOrientation];
NSLog(@"shouldAutorotateToInterfaceOrientation: %s (%@)",
result ? "YES" : "NO",
orientationString);
return result;
}
仍有一个令人讨厌的问题,即Segue动画不使用当前方向。 我猜想,对每个VC进行子类化并在推送时放置一些方向/在弹出时通知代理可能是解决问题的方法。
还有重要的一点:
shouldAutorotateToInterfaceOrientation不起作用
横屏模式下的tabBarController和navigationControllers,第II集