如何在IOS7中处理多视图上的设备方向?

时间:2014-03-14 10:00:23

标签: ios iphone objective-c cocoa-touch orientation

我正在开发一个应用程序,其中我有两个ViewControllers名称为VC1和VC2,VC1应该支持纵向和横向方向,纵向应该显示地图和横向它应该显示列表。现在对于VC2,它仅适用于纵向模式。 我正在为两个ViewControllers附加图像 enter image description here

enter image description here

在第二张图片中Google地图应该是纵向的并且在横向列表中,但是当我将方向从纵向更改为横向时,列表仅在纵向模式下生成。下面是处理方向的代码

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

2 个答案:

答案 0 :(得分:0)

如果你的rootViewController是UINavigationController,那么你需要使用UINavigationController的类别。像

<强>的UINavigationController + autoRotate.h

#import <UIKit/UIKit.h>

@interface UINavigationController (autoRotate)

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
-(BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;

@end

<强>的UINavigationController + autoRotate.m

#import "UINavigationController+autoRotate.h"

@implementation UINavigationController (autoRotate)

-(NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

-(BOOL)shouldAutorotate
{
    return self.topViewController.shouldAutorotate;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

@end

然后在 viewController。

中使用以下方法
#pragma mark -
#pragma mark - UIInterfaceOrientation Methods

// for iOS 6 and latter
-(BOOL) shouldAutorotate
{
    // return as you want;
    return YES;
}

// for iOS 5 and lower
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // return as you want;
    return  YES;
}


- (NSUInteger)supportedInterfaceOrientations
{
    //UIInterfaceOrientationMask as you want 
    return UIInterfaceOrientationMaskPortrait;
}

答案 1 :(得分:0)

如果您在应用UINavigationController中使用-(BOOL)shouldAutorotate,则不会在ViewControllers中调用UINavigationController。在这种情况下,您必须继承@interface RotationAwareNavigationController : UINavigationController @end @implementation RotationAwareNavigationController -(NSUInteger)supportedInterfaceOrientations { UIViewController *top = self.topViewController; return top.supportedInterfaceOrientations; } -(BOOL)shouldAutorotate { UIViewController *top = self.topViewController; return [top shouldAutorotate]; } @end

{{1}}