我看到了这个 stackoverflow 帖子:shouldAutorotateToInterfaceOrientation is not working in iOS 6。答案是添加一个名为 RotationIn_IOS6 的类别。我已经这样做了,不同视图控制器的视图在 iOS6 中正常工作。问题出在iOS5中。
我只需要几个视图来向各个方向旋转,其余的应该是纵向的或者是 PortraitUpsideDown。我面临的问题是要么全部不旋转(code1),要么在旋转到横向后,它保持相同的方向,直到您旋转回纵向(code2)。
代码1:
@implementation UINavigationController (RotationIn_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
@end
代码 2:
@implementation UINavigationController (RotationIn_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if([self.visibleViewController isKindOfClass:[MyClassToRotate class]])
{
return [self.visibleViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
else
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
}
在这方面需要一些指导。不知道如何解决这个问题。
编辑:
代码3:
1) 从类别中删除了 shouldAutorotateToInterfaceOrientation。2)将此代码添加到我的特定课程中
- (BOOL)shouldAutorotate
{
//returns true if want to allow orientation change
return TRUE;
}
- (NSUInteger)supportedInterfaceOrientations
{
//decide number of origination tob supported by Viewcontroller.
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
//from here you Should try to Preferred orientation for ViewController
return TRUE;
}
结果 -> 所有视图控制器都锁定为纵向。无法访问 PortraitUpsideDown。一旦进入我的视图控制器,它就可以旋转,但是当你离开视图控制器时,它会锁定在横向中。
编辑2:
每个视图控制器都包含以下代码:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate {
return NO;
}