使用NavigationController在不同视图上的不同方向

时间:2013-04-30 19:22:10

标签: iphone ios

我已经subClassed我的NavigationController并添加了这段代码:

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft |UIInterfaceOrientationMaskPortrait;
} 
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft;
}

我有2个视图控制器。如何在不能旋转的情况下在一个和纵向模式下强制横向模式? (所有的标志都在plist中启用)

3 个答案:

答案 0 :(得分:0)

假设您正在使用iOS6,请更改自定义导航控制器以使其具有此功能(此解决方案似乎只适用于模式或全屏演示):

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

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

然后在您希望拥有Landscape添加的控制器中

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

和另一个

    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }

    - (BOOL)shouldAutorotate
    {
        return YES;
    }

答案 1 :(得分:0)

我正在考虑仅针对iOS 6.0及更高版本进行轮播。为UINaviagtionController创建类别可能是一个很好的方法。请按照以下步骤操作

步骤1.将此代码放入AppDelegate.m

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        return UIInterfaceOrientationMaskAll;
    }

步骤2.为UINaviagtionController创建一个类别

Orientation.h

    #import <Foundation/Foundation.h>

    @interface UINavigationController (Orientation)

    @end

Orientation.m

    #import "Orientation.h"

    @implementation UINavigationController (Orientation)

    - (BOOL)shouldAutorotate {
       if (self.topViewController != nil)
            return [self.topViewController shouldAutorotate];
       else
            return [super shouldAutorotate];
     }

    - (NSUInteger)supportedInterfaceOrientations {
        if (self.topViewController != nil)
            return [self.topViewController supportedInterfaceOrientations];
        else
            return [super supportedInterfaceOrientations];
    }

    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        if (self.topViewController != nil)
            return [self.topViewController preferredInterfaceOrientationForPresentation];
        else
            return [super preferredInterfaceOrientationForPresentation];
    }

步骤3.现在创建一个强制初始方向的UIViewController类。对于肖像

PortraitVC.h

    #import <UIKit/UIKit.h>

    @interface PortraitVC : ViewController

    @end

PortraitVC.m

    - (BOOL)shouldAutorotate
    {
        return NO;
    }

    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
       return UIInterfaceOrientationPortrait;
    }

类似于景观

LandscapeVC.h

    #import <UIKit/UIKit.h>

    @interface LandscapeVC : ViewController

    @end

LandscapeVC.m

    - (BOOL)shouldAutorotate
    {
        return NO;
    }

    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskLandscape;
    }
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
       return UIInterfaceOrientationLandscapeLeft;
    }

步骤4.现在您想要仅在纵向模式下强制而不进行旋转的原始ViewController,将此代码写入viewDidLoad

    PortraitVC *c = [[PortraitVC alloc] init];
    [self presentViewController:c animated:NO completion:NULL];
    [self dismissViewControllerAnimated:NO completion:NULL];

并设置这样的方向

    - (BOOL)shouldAutorotate
    {
        return NO;
    }

    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
       return UIInterfaceOrientationPortrait;
    }

如果您只想在横向模式下强制原始ViewController而没有旋转,请在viewDidLoad中编写此代码

    LandscapeVC *c = [[LandscapeVC alloc] init];
    [self presentViewController:c animated:NO completion:NULL];
    [self dismissViewControllerAnimated:NO completion:NULL];

并设置这样的方向

    - (BOOL)shouldAutorotate
    {
        return NO;
    }

    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskLandscape;
    }
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
       return UIInterfaceOrientationLandscapeLeft;
    }

我这样做是因为只有在使用presentViewController或presentModalViewController时才会调用“preferredInterfaceOrientationForPresentation”。并且要设置初始方向,必须调用“preferredInterfaceOrientationForPresentation”。

希望这有帮助。

答案 2 :(得分:0)

因此,经过大量的追踪和错误,我找到了一个可以解决的解决方案。

在你的AppDelegate中:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;

    if(self.window.rootViewController){
        UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presentedViewController supportedInterfaceOrientations];
    }

    return orientations;
}

这将控制根视图控制器的方向。

DataManager.h中的

(或者您正在使用的任何其他单例也可以使用NSUserDefaults。

@property (nonatomic, strong) NSString *currentView;

在ViewController.m(root)中:

-(NSUInteger)supportedInterfaceOrientations
{

    if([[DataManager sharedDataManager].currentView isEqualToString:@"Trailer"])
    {
        return UIInterfaceOrientationMaskLandscapeRight;
    }
    else if([[DataManager sharedDataManager].currentView isEqualToString:@"Menu"])
    {
        return UIInterfaceOrientationMaskPortrait;
    }

    return UIInterfaceOrientationMaskPortrait;
}

从根ViewController,您可以访问应用中所有ViewControlles的所有方向。如果您想将一个ViewController限制为横向,另一个仅限于纵向,则非常有用。您需要添加到每个ViewController的唯一内容是ViewWillAppear内部要为ViewController提供的名称,如下所示:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [DataManager sharedDataManager].currentView = @"Menu";
}

使用此解决方案,您将无法获得有关在视图之间切换的恼人警告,就像您使用当前\ dissmis视图解决方案强制轮换一样。 也许有一种方法可以更好地识别ViewController,我很想听听。如果您发现此解决方案有任何问题,我也很想听听。 感谢