Objective-C中iOS 6上的强制横向方向

jay*_*ixz 7 xcode landscape objective-c uinavigationcontroller

我有一个UINavigationController内的主视图控制器.在那个主视图控制器中,我有一个按钮,用于推送一个内部有UIWebView的详细视图控制器.我希望这个细节视图控制器在加载时处于横向模式.回到主视图控制器,它再次强制返回到纵向模式.我正在运行iOS 6.

我已经看到了其他类似的问题,但它并不适合我.我创建了一个LandscapeViewController,它是UIViewController的子类,我在这里编写了这些方法:

#pragma mark - Orientation Methods
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (BOOL)shouldAutorotate
{
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

当我按下详细视图控制器时,这是我的代码:

DetailViewController *detailVC = [[DetailViewController alloc]
                                      initWithNibName:@"DetailViewController"
                                      bundle:nil];

    [self.navigationController pushViewController:detailVC
                                         animated:YES];
Run Code Online (Sandbox Code Playgroud)

我正在考虑在上面的代码中将我的LandscapeViewController子类化到哪里以使其工作或如何正确地子类化并推送我的详细视图控制器.如果导航控制器无法将我的细节视图控制器从纵向推送到横向,我也可以模态显示我的详细视图控制器.我在哪里做错了?

fdi*_*iaz 8

考虑:视图A:仅限纵向 - 视图B:仅景观

我无法在导航控制器中执行此操作.相反,我所做的是从视图A打开模态视图以查看B并强制新的导航控制器进入此视图.

这在iOS5 +中适用于我.

您需要为导航控制器创建一个类别,如下所示:

UINavigationController的+ Rotation_IOS6.h

#import <UIKit/UIKit.h> 

@interface UINavigationController (Rotation_IOS6)

- (BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;

@end
Run Code Online (Sandbox Code Playgroud)

UINavigationController的+ Rotation_IOS6.h

#import "UINavigationController+Rotation_IOS6.h"

@implementation UINavigationController (Rotation_IOS6)

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

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

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

@end
Run Code Online (Sandbox Code Playgroud)

AppDelegate.m中添加:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return UIInterfaceOrientationMaskAll;
}
Run Code Online (Sandbox Code Playgroud)

然后在视图A中:

- (BOOL)shouldAutorotate {
    return YES;
}

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
Run Code Online (Sandbox Code Playgroud)

同样在View A中打开View B执行此操作:

ViewB *vc = [[ViewB alloc] initWithNibName:@"ViewB" bundle:nil];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];

[self presentViewController:navigationController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

最后,在视图B中

- (BOOL)shouldAutorotate {
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
Run Code Online (Sandbox Code Playgroud)