为什么使用UINavigationController时无法强制横向?

我发现很多问题强制UIViewController风景作为默认值: 如何强制UIViewController纵向在iOS 6中试试这个:

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

但是当我在UINavigationController里面使用UIViewController的时候,我不能强迫使用landscape.Please help!

*工作不好

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController]; [navControl setNavigationBarHidden:YES]; self.window.rootViewController = navControl; [self.window makeKeyAndVisible]; return YES; } 

*工作正常:

 self.window.rootViewController = self.viewController; 

最好的方法是创build扩展UINavigationController,并在扩展类中写入方向函数。

类声明:

 @interface MyNavigationControllerViewController : UINavigationController @property(nonatomic, assign) UIInterfaceOrientation orientation; @property(nonatomic, assign) NSUInteger supportedInterfaceOrientatoin; @end 

MyNavigationController的实现

 @implementation MyNavigationController @synthesize supportedInterfaceOrientatoin = _supportedInterfaceOrientatoin; @synthesize orientation = _orientation; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization _supportedInterfaceOrientatoin = UIInterfaceOrientationMaskLandscape; _orientation = UIInterfaceOrientationLandscapeLeft; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (BOOL)shouldAutorotate { return YES; } -(NSUInteger)supportedInterfaceOrientations { return _supportedInterfaceOrientatoin; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return self.orientation; } @end 

并使用你的扩展像MyNavigationController作为导航控制器到你的rootviewcontroller。

 @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) ViewController *viewController; @property (strong, nonatomic) MyNavigationControllerViewController *myNavController; - (void) reloadAppDelegateRootViewControllerLandscape; - (void) reloadAppDelegateRootViewController; @end 

所以你的应用程序委托didfinishlounchingwith选项代码将如下。

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; self.myNavController = [[MyNavigationController alloc] initWithRootViewController:self.viewController]; [self.myNavController setNavigationBarHidden:YES]; self.window.rootViewController = self.myNavController; [self.window makeKeyAndVisible]; return YES; } 

只有这样,您可以使用相同的导航控制器为两个不同的视图提供不同的方向,以重新加载导航控制器本身。 所以如果你添加两个方法

 - (void) reloadAppDelegateRootViewController{ [[[UIApplication sharedApplication].delegate window] setRootViewController:nil]; [(MyNavigationControllerViewController *)self.myNavController setOrientation:UIInterfaceOrientationPortrait]; [(MyNavigationControllerViewController *)self.myNavController setSupportedInterfaceOrientatoin:UIInterfaceOrientationMaskAll]; [[[UIApplication sharedApplication].delegate window] setRootViewController:self.myNavController]; } - (void) reloadAppDelegateRootViewControllerLandscape{ [[[UIApplication sharedApplication].delegate window] setRootViewController:nil]; [(MyNavigationControllerViewController *)self.myNavController setOrientation:UIInterfaceOrientationLandscapeLeft]; [(MyNavigationControllerViewController *)self.myNavController setSupportedInterfaceOrientatoin:UIInterfaceOrientationMaskLandscape]; [[[UIApplication sharedApplication].delegate window] setRootViewController:self.myNavController]; } 

并在推送和popup视图后调用这些函数。

注意: – 我不知道这是一个好方法还是坏方法。

你需要去你的plist文件并设置方向。 你也可以在你的项目文件中做到这一点。 虽然plist方向设置将覆盖所有。

在您的plist中,您可以将两个方向选项设置为左侧风景button和右侧风景button。