在纵向模式下保持一个视图控制器,在横向模式iOS中保持其他

时间:2013-12-04 10:01:09

标签: ios iphone objective-c

我有一个带有2个视图控制器的iOS应用程序,即FirstViewControllerSecondViewController我的窗口的rootViewController是UINavigationController

FirstViewController应仅在纵向模式下工作,而SecondViewController仅在横向模式下工作。

搜索遍布Stackoverflow我发现对于iOS6及更高版本,我必须创建一个超过UINavigationController的类别并覆盖-supportedInterfaceOrientations

问题

从FirstViewController开始。现在我的手机处于纵向模式,我按下SecondViewController,视图以纵向模式加载。一旦我将手机旋转到横向,视图将旋转为横向(从此点开始将不会返回到纵向)。

当我弹回时,FirstViewController将再次出现在Portrait中(无论手机的方向如何)。

我希望SecondViewController根本不应以纵向模式显示。我整天绞尽脑汁......无法找到解决方案。

的appDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    ViewController *vc = [[ViewController alloc] init];

    self.navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
    [self.navigationController setNavigationBarHidden:YES];
    self.window.rootViewController = self.navigationController;

    [self.window makeKeyAndVisible];

    return YES;
}

FirstViewController

- (IBAction)btnClicked:(id)sender
{
    SecondViewController *vc = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:vc animated:NO];
}

#pragma mark - Rotation handlers
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

SecondViewController

- (IBAction)btnClicked:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark - Rotation handlers
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}

UINavigation类别

@implementation UINavigationController (Rotation)

-(BOOL)shouldAutorotate
{
    //return [self.topViewController shouldAutorotate];
    return YES;
}


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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}
@end

2 个答案:

答案 0 :(得分:0)

嗯,这有点晚了,但这就是我的想法。

虽然有很多解决方案适用于FirstVC是Portrait和Second可以是人像和风景的情况,但我找不到任何解决此问题的好方法(仅限第一张肖像和仅第二张风景)。这是我做的:

将两个视图控制器嵌入到自己的导航控制器中。创建两个新类,比如说FirstNavController和SecondNavController子类化UINavigationController。使用这些作为您的导航控制器。 (如果您使用的是StoryBoards,请选择导航控制器,转到Identity Inspector并更改' Class'字段。)

现在,在FirstNavController中,添加:

- (BOOL)shouldAutorotate 
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait;
}

在SecondNavController中:

- (BOOL)shouldAutorotate 
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscape;
}

您必须以模态方式呈现SecondNavController。

您的视图控制器无需任何操作。确保在应用程序设置中添加所有必需的方向。

此方法的唯一缺点是两个视图不在同一个导航堆栈中,因为第二个视图是以模态方式呈现的,因此您不会看到后退按钮。但是你可以自己添加一个取消/关闭按钮,并在SecondVC中调用dismissViewControllerAnimated。

答案 1 :(得分:-3)

在我以前的应用程序中我已经完成了

首先,您需要启用纵向,横向左侧,横向右侧方向投影

现在

将下面的代码设置为FirstViewController

-(void) viewWillAppear:(BOOL)animated{
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]; 
 }
      - (BOOL)shouldAutorotate
 {
       return NO;
 }

将下面的代码设置为你的secondViewController

#define degreesToRadian(x) (M_PI * (x) / 180.0)


 @implementation UINavigationController (Rotation_IOS6)

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

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

 @end
 @interface secondViewController ()

 @end

 @implementation secondViewController

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
    self.view.transform = CGAffineTransformIdentity;
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));

 }
 - (BOOL)shouldAutorotate
{
   return YES;

 }

 - (NSUInteger)supportedInterfaceOrientations
 {
    return UIInterfaceOrientationMaskLandscape;
 }


  @end