0

在我的应用程序中,四个 viewcontrollers 在 potrait 模式下逐个导航,但我在最终 viewcontroller 中添加了coverflow,当模拟器旋转时它应该变成横向。我在 plist 中选择了所有方向,除了 Upsidedown 方向,这样coverflow 工作正常在风景中,但所有其他视图控制器在旋转时也会进入风景,但是即使模拟器旋转,我也需要这些视图控制器处于 potrait 中。我尝试了很多代码,例如,

    shouldAutorotate  and supportedInterfaceOrientations.

但是我没有得到明确的解决方案来解决我的期望,如果有任何想法,将不胜感激。

4

4 回答 4

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;
  };
}
于 2013-05-03T07:33:34.393 回答
0

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

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

于 2013-05-03T07:33:23.863 回答
0

Add new Objective-C class (subclass of UINavigationController) and add the following code to the .m files

-(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];
  }

After you added the new classes go to your ViewController classes and make the following changes

- (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

In the shouldAutorotate , shouldAutorotateToInterfaceOrientation: return YES if you want the ViewController to be supporting Multiple orientation else return NO , also in houldAutorotateToInterfaceOrientation: method pass the Orintation you want for that specific ViewController , Repeat the same for all the view controllers .

Reason of doing this:-

1:Although you can change the preferredInterfaceOrientationForPresentation: of any viewController to a specific orientation but since you are using the UINavigationController you also need to override the supportedInterfaceOrientations for your UINavigationController

2:In order the override the supportedInterfaceOrientations for UINavigationController we have subclassed UINavigationController and modified the method related to the UINavigation Orientation.

Hope it will help you !

于 2013-05-03T07:38:40.370 回答
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);
}
于 2013-05-03T10:01:58.577 回答