2

I'm trying to restrict one view controller which on top of UINavigationController. To do that i've created a UINavigationController subclass and implemented 2 methods

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

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

I want the first viewcontroller on top of UINavigationController(which is Root View Controller) should be in portrait mode and the next view controller which i'm pushing from the root view controller should be Landscape mode(ONLY).

So i'm overriding those two methods in both view controllers. In the root view controller

- (BOOL)shouldAutorotate {
return NO;}

- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;}

In the next view controller

- (BOOL)shouldAutorotate {
return YES;}

- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;}

Its working fine but not completely. For the first time when I push the view controller its showing in portrait mode(Not restricting to landscape as I expected) and once I rotate the device/simulator and its working fine and restricting to landscape only.

Can anyone help in this?

4

4 回答 4

1

Try this out.

Call this one in the viewWillAppear will explicitly tell the device to jump to the portrait orientation.

[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];

I don't think this is the right solution. But if you got no other options, you can use this. Happy Coding :)

于 2012-11-06T09:43:49.533 回答
0

U present new controller :

SecondViewController *objSecondViewController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController presentViewController:objSecondViewController animated:NO completion:^{}];

In new controller :

- (BOOL)shouldAutorotate {
  return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
  return UIInterfaceOrientationLandscapeLeft;
}
于 2012-11-06T09:44:52.667 回答
-1

这对我有用。试试用这个:

1)YourApp-Info.plist

  • 为支持的界面方向再添加一个数组
  • 将您需要的方向添加到这本词典

参考下面的截图:

在此处输入图像描述

2)Project Target

  • 从支持的接口方向中选择所需的方向

参考下面的截图:

在此处输入图像描述

于 2012-11-06T10:08:40.727 回答
-2

UIViewController具有以下功能。您可以在要限制纵向的视图控制器中实现此功能。

  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations

    if(interfaceOrientation == UIInterfaceOrientationPortrait)
        return YES;
    else
        return NO;

    }
于 2012-11-06T09:39:26.733 回答