方向应仅针对选定的viewcontroller进行更改

时间:2013-05-03 07:22:19

标签: iphone ios

在我的应用程序中,四个viewcontrollers在potrait模式下接下来导航,但是我在最终的viewcontroller中添加了coverflow,当模拟器旋转时它应该是landscape.I选择plist中的所有方向执行Upsidedown方向,这样coverflow在Landscape中运行良好,但所有其他的viewcontrollers也在rotatad时进入景观,但是我需要这些viewcontrollers在potrait中,即使模拟器是旋转的。我尝试了许多代码,例如,

    shouldAutorotate  and supportedInterfaceOrientations.

但是,如果有任何想法,我无法明确解决我期待的问题。

4 个答案:

答案 0 :(得分:1)

将观察者添加到要旋转的视图的viewDidLoad方法,如下所示:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
 addObserver:self selector:@selector(orientationChanged:)
 name:UIDeviceOrientationDidChangeNotification
 object:[UIDevice currentDevice]];

然后根据 orientationChanged 方法中的横向视图设置视图,如下所示:

- (void) orientationChanged:(NSNotification *)note{
UIDevice * device = [UIDevice currentDevice];
switch(device.orientation)
{
    case UIDeviceOrientationPortrait:

        break;
    case UIDeviceOrientationPortraitUpsideDown:

        break;
    case UIDeviceOrientationLandscapeLeft:

        break;
    case UIDeviceOrientationLandscapeRight:

        break;

    default:
        break;
  };
}

答案 1 :(得分:0)

在iOS 6及更高版本中,负责旋转的视图控制器是容器ViewControllers,例如UINavigationController& UITabBarController。你在项目中使用什么作为rootviewcontroller?

我编写了导航控制器类别here,您可以在其中处理各个视图控制器支持的接口方向。

答案 2 :(得分:0)

添加新的Objective-C类(UINavigationController的子类)并将以下代码添加到.m文件

-(NSUInteger)supportedInterfaceOrientations
 {
     NSLog(@"supportedInterfaceOrientations = %d ", [self.topViewController         supportedInterfaceOrientations]);

     return [self.topViewController supportedInterfaceOrientations];
 }

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

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
    // You do not need this method if you are not supporting earlier iOS Versions

    return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
  }

添加新类后,转到ViewController类并进行以下更改

- (BOOL)shouldAutorotate  // iOS 6 autorotation fix
  {
    return YES;
  }
- (NSUInteger)supportedInterfaceOrientations // iOS 6 autorotation fix
  {
      return UIInterfaceOrientationMaskAll;
  }

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation // iOS 6 autorotation fix
  {
      return UIInterfaceOrientationPortrait;
  }
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  {
      return YES;
  }

enter image description here

在shouldAutorotate中,shouldAutorotateToInterfaceOrientation:如果你想让ViewController支持多个方向,则返回YES,否则返回NO,同样在houldAutorotateToInterfaceOrientation中:方法传递你想要的特定ViewController的Orintation,对所有视图控制器重复相同的操作。

这样做的原因: -

1:虽然您可以将所有viewController的preferredInterfaceOrientationForPresentation更改为特定方向,但由于您使用的是UINavigationController,因此您还需要覆盖UINavigationController的supportedInterfaceOrientations

2:为了覆盖UINavigationController的supportedInterfaceOrientations,我们已经将UINavigationController子类化,并修改了与UINavigation Orientation相关的方法。

希望它会对你有所帮助!

答案 3 :(得分:0)

viewDidLoad或viewWillApper方法添加以下代码

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

设置方向

#if (__IPHONE_6_0 >= __IPHONE_OS_VERSION_MAX_ALLOWED)
- (NSInteger)supportedInterfaceOrientations {


    return (UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskPortrait);


}
#endif

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return (interfaceOrientation==UIInterfaceOrientationPortrait);
}