0

我正在使用 XCode 4.6 和 iOS 6.1。但我的应用程序不会改变方向。我已将我的应用程序设置为支持 pList 中的方向并添加了方向更改方法。但该应用程序仍然没有改变方向。

在此处输入图像描述

在此处输入图像描述

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}

我正在使用 TabBarController,并使用此代码添加它。

self.window.rootViewController = self.tabController;

在 iOS 6 模拟器中一切正常,但在 iOS 5 中它不起作用。

4

4 回答 4

2

检查方向的代码是否有效?

代码 ::

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
    ......
}

检查方法::

-(void)orientationChanged {

   NSLog(@"Orientation Changed..!!");
}

方向改变方法的代码

在 PCH 文件中,

#define IOS_OLDER_THAN_6        ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] < 6.0 )
#define IOS_NEWER_OR_EQUAL_TO_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 6.0 )

在 .m 文件中,

//For Less than IOS 6

#ifdef IOS_OLDER_THAN_6
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation
{
   return toInterfaceOrientation;
}
#endif

// For Newer than IOS 6.
#ifdef IOS_NEWER_OR_EQUAL_TO_6
-(BOOL)shouldAutorotate
{
   return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
   return (UIInterfaceOrientationMaskAll);
}
#endif

希望对您有所帮助。

谢谢。

于 2013-03-15T12:04:22.147 回答
1

您需要使用这两种新方法来控制和配置 viewControllers 的方向更改:

- (BOOL)shouldAutorotate {
    return YES;    
}

- (NSUInteger)supportedInterfaceOrientations {    
    return (UIInterfaceOrientationMaskPortrait | 
            UIInterfaceOrientationMaskPortraitUpsideDown);
}

您正在实施的方法适用于 iOS 5.x ,但此答案中提到的方法适用于 iOS 6.x。如果你想要两个操作系统版本的兼容性,那么你需要在你的 viewController 中实现这两个方法。

于 2013-03-15T11:49:57.900 回答
0

在最顶层的导航控制器中,定义:

-(BOOL)shouldAutorotate {return [self.visibleViewController shouldAutorotate];}

在你的下一个视图控制器中:

-(BOOL)shouldAutorotate {return YES;}

-(NSUInteger)supportedInterfaceOrientations {  
return (UIInterfaceOrientationMaskPortrait | 
        UIInterfaceOrientationMaskPortraitUpsideDown);
}
于 2013-03-15T12:34:32.287 回答
0

您正在使用tabBar,因此将此代码放在@end 底部的AppDelegate 中。

@implementation UITabBarController (Rotation)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{   
    if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) {
        UIViewController *rootController = [((UINavigationController *)self.selectedViewController).viewControllers objectAtIndex:0];
        return [rootController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    }
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

@end

希望这会有所帮助。

于 2013-03-16T05:29:49.053 回答