某些视图控制器的固定方向

时间:2014-02-13 07:32:09

标签: ios7 uinavigationcontroller uitabbarcontroller uiinterfaceorientation

在我的应用程序中,我同时有tabbar和navigationBar。 rootview controller tabbar和tabbar有4个导航控制器。

我想让一些viewcontrollers只是肖像。这可能是一个常见的问题,但我已经尝试了,但我无法解决这个问题。

如何为某些视图控制器设置纵向?

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题并回答,如果有人遇到同样的问题,他们可以得到帮助。

shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation

如果viewController位于navigationcontroller的任何tabbarcontroller中,则上述方法不会被调用。如果在tabbarcontroller或导航控制器中声明了这些方法,那么它们将被调用。在我的例子中,viewcontrollers位于navigationcontroller中,导航控制器位于tabbarcontroller内。

为了解决这个问题,我创建了一个类FixedOrientationTab,它是UITabBarController的子类,以及一个导航类OrientationEnabledNavigation,它是UINavigationController的子类。然后我在FixedOrientationTab和OrientationEnabledNavigation中实现了shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation个方法。

<强> OrientationEnabledNavigation.h

#import <UIKit/UIKit.h>

@interface OrientationEnabledNavigation : UINavigationController

@end

<强> OrientationEnabledNavigation.m

#import "OrientationEnabledNavigation.h"

@interface OrientationEnabledNavigation ()

@end

@implementation OrientationEnabledNavigation

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

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

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

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

<强> FixedOrientationTab.h

#import <UIKit/UIKit.h>

@interface FixedOrientationTab : UITabBarController

@end

<强> FixedOrientationTab.m

#import "FixedOrientationTab.h"

@interface FixedOrientationTab ()

@end

@implementation FixedOrientationTab

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
    //    return NO;
}

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

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


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

然后,如果要在项目中使用导航控制器,请使用OrientationEnabledNavigation和tabbar FixedOrientationTab。之后如果你在viewcontroller中实现shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation这些方法,那么它们就会被调用。

希望这会有所帮助.. :)