在纵向模式iOS中维护一个视图控制器,在其他模式下维护 [英] Maintain one view controller in Portrait and other in Landscape mode iOS

查看:124
本文介绍了在纵向模式iOS中维护一个视图控制器,在其他模式下维护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an iOS app with 2 view controllers namely - FirstViewController and SecondViewController. My window's rootViewController is UINavigationController.

FirstViewController只能在纵向模式下工作,而SecondViewController只在横向模式下工作。

FirstViewController is supposed to work ONLY in portrait mode and SecondViewController ONLY in Landscape mode.

搜索遍布Stackoverflow我发现对于iOS6及以上版本我必须在 UINavigationController 上创建一个类别并覆盖 -supportedInterfaceOrientations

Searching all over Stackoverflow I found out that for iOS6 and above I have to create a category over UINavigationController and override -supportedInterfaceOrientations

问题

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

Starting from FirstViewController. Now my phone being in Portrait mode, I push SecondViewController, the view loads in portrait mode. Once I rotate my phone to be in landscape the view will rotate to landscape ( and from this point onwards will not return to portrait at all ).

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

When I pop back FirstViewController will be in Portrait again ( no matter what the orientation of the phone ).

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

I want that SecondViewController shouldn't be displayed in Portrait mode at all. I've racked my brains all day...cannot find a solution.

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


推荐答案

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

Well, it's a bit late but here is what I figured.

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

While there are many solutions available for the case when FirstVC is Portrait and Second can be Portrait and Landscape, I couldn't find any good solution to this problem (First Portrait ONLY and Second Landscape ONLY). Here is what I did:

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

Embed both view controllers in their own Navigation Controllers. Create two new classes, say FirstNavController and SecondNavController subclassing UINavigationController. Use these as your navigation controllers. (If you are using StoryBoards, select the Navigation Controller, go to Identity Inspector and change 'Class' field).

现在,在FirstNavController中,添加:

Now, in FirstNavController, add:

- (BOOL)shouldAutorotate 
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait;
}

在SecondNavController中:

And in SecondNavController:

- (BOOL)shouldAutorotate 
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscape;
}

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

You will have to present the SecondNavController modally.

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

Nothing needs to be done in your View Controllers. Make sure you add all required orientations in your application settings.

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

The only drawback of this method is that both views are not in the same navigation stack, as second was presented modally, so you won't see a back button. But you can add a cancel/dismiss button yourself and call dismissViewControllerAnimated in SecondVC.

这篇关于在纵向模式iOS中维护一个视图控制器,在其他模式下维护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆