【发布时间】:2012-10-27 12:52:14
【问题描述】:
我制作了一个针对 iOS 5 和 iOS 6 的应用程序,由于某种原因,它仅在运行 iOS 6 的设备上(无论是在 iPhone 还是 iPad 上)使用时才会旋转,并且不会在使用 iOS 5 的设备上旋转. 我的应用程序是通用的。请帮我解决这个问题!谢谢
【问题讨论】:
标签: xcode ios5 ios6 xcode4.5 autorotate
我制作了一个针对 iOS 5 和 iOS 6 的应用程序,由于某种原因,它仅在运行 iOS 6 的设备上(无论是在 iPhone 还是 iPad 上)使用时才会旋转,并且不会在使用 iOS 5 的设备上旋转. 我的应用程序是通用的。请帮我解决这个问题!谢谢
【问题讨论】:
标签: xcode ios5 ios6 xcode4.5 autorotate
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return ((toInterfaceOrientation == UIInterfaceOrientationPortrait) || (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}
在所有 uiviewcontroller 子类中覆盖这两个方法......这适用于 ios 6 及更早版本
【讨论】:
iOS 5 和 iOS 6 调用不同的方向和旋转代理。在 iOS 5 中,实现:
shouldAutorotateToInterfaceOrientation:,在 iOS 6 中已弃用。
因此,在 iOS 6 中,请确保您设置了根视图控制器,并实现:
shouldAutorotate:、supportedInterfaceOrientations 和supportedInterfaceOrientationsForWindow:
【讨论】:
我遇到了类似的问题。你可以查看这个问题的答案:
Rotation behaving differently on iOS6
总而言之,要让自动旋转在 iOS 5 和 iOS 6 中充分发挥作用,并且还要处理 PortraitUpsideDown 方向,我必须实现一个自定义 UINavigationController 并将其分配给应用程序委托 didFinishLaunchingWithOptions 方法中的 self.window.rootViewController。
【讨论】:
shouldAutorotateToInterfaceOrientation 在 ios6 中已弃用。
所以如果你想在两个操作系统版本上运行应用程序,那么也添加 shouldAutorotateToInterfaceOrientation 如下
//for ios6
- (BOOL)shouldAutorotate {
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIInterfaceOrientationLandscapeLeft ||orientation == UIInterfaceOrientationLandscapeRight )
{
return YES;
}
return NO;
}
//for ios5
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//interfaceOrientation == UIInterfaceOrientationLandscapeRight;
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||interfaceOrientation == UIInterfaceOrientationLandscapeRight ) {
return YES;
}
return NO;
}
【讨论】:
为了在 ios5 和 ios6 中支持自动旋转,我们需要在 ios6 的情况下提供回调......`[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
我们需要打电话
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
-(BOOL)shouldAutoRotate{
return YES;
}
ios5
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{ return ((toInterfaceOrientation == UIInterfaceOrientationPortrait) || (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)); }
【讨论】: