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

Ios7 某些视图控制器的固定方向,ios7,uinavigationcontroller,uitabbarcontroller,uiinterfaceorientation,Ios7,Uinavigationcontroller,Uitabbarcontroller,Uiinterfaceorientation,在我的应用程序中,我有tabbar和navigationBar。rootview控制器tabbar和tabbar有4个导航控制器 我想使一些ViewController仅为纵向。这可能是一个常见的问题,但我已经尝试了足够多,但我无法解决这个问题 如何为某个视图控制器设置纵向方向?我已经解决了这个问题并回答了,这样,如果有人遇到同样的问题,他们可以得到帮助 shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOri

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

我想使一些ViewController仅为纵向。这可能是一个常见的问题,但我已经尝试了足够多,但我无法解决这个问题


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

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

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

为了解决这个问题,我创建了一个类FixedOrientationTab,它是UITabBarController的子类,一个导航类OrientationEnabledNavigation,它是UINavigationController的子类。然后我实现了shouldAutorotate,支持接口方向,首选接口方向在FixedOrientationTab和OrientationEnabledNavigation中的表示方法

定向导航

固定定向选项卡

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

希望这有助于

#import <UIKit/UIKit.h>

@interface OrientationEnabledNavigation : UINavigationController

@end
#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
#import <UIKit/UIKit.h>

@interface FixedOrientationTab : UITabBarController

@end
#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