【问题标题】:How to change the orientation for a single view not for all views in ipad?如何更改单个视图的方向而不是 ipad 中的所有视图?
【发布时间】:2013-11-28 05:39:17
【问题描述】:

我对 IOS 比较陌生。在我的 iPad 应用程序中,我有 6 个视图。默认情况下,我将所有屏幕的方向固定为横向模式。但是当我打开那个特定的视图时,我的第 5 个视图必须像纵向模式一样更改,是否有任何程序代码来更改该特定视图方向?请你帮我解决这个问题。提前致谢。

【问题讨论】:

  • 您使用的是哪个 iOS 版本?

标签: objective-c ipad ios7 uiinterfaceorientation


【解决方案1】:

1.需要通过调用生成DeviceOrientation通知

-(void)beginGeneratingDeviceOrientationNotifications 

UIDevice 的方法。

2.在控制视图的UIViewController中添加通知观察

[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rotateMyUIView:) name:UIDeviceOrientationDidChangeNotification object:nil];

3.in rotateMyUIView 使用方法旋转你的 UIView

myView.transform = CGAffineTransformMakeRotation(M_PI/2); // you must check orientation first 

【讨论】:

    【解决方案2】:

    您可以在您不想受到影响的特定视图控制器中实现didRotateFromInterfaceOrientation

    【讨论】:

      【解决方案3】:

      iOS 6 之后你可以这样做..

      在 AppDelegate.h 文件中

      @interface AppDelegate : UIResponder <UIApplicationDelegate>
      {
          BOOL allowRotation;
      }
      

      在 AppDelegate.m 文件中

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
          allowRotation = NO;  
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeDeviceOrientation:) name:@"ChangeDeviceOrientation" object:nil];
      
          return YES;
      }
      
      - (void) changeDeviceOrientation:(NSNotification*)notification {
         if(allowRotation)
         {
             allowRotation = NO;
         }else
         {
             allowRotation = YES;
         }
      }
      
      -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
      {
          NSLog(@"Not FullScreen");
          if (allowRotation) {
              NSLog(@"FullScreen");
              return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
          }
          return UIInterfaceOrientationMaskPortrait;
      }
      

      并在您的视图控制器(您要更改方向的哪个视图C)中调用此通知

      -(void)viewWillDisappear:(BOOL)animated{
          [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeDeviceOrientation"   object:nil];
      }
      
      -(void)viewWillAppear:(BOOL)animated
      {
          [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeDeviceOrientation"   object:nil];
      }
      

      【讨论】:

      • 根据您的代码,从应用程序声明开始,所有屏幕仅显示纵向模式。但我的要求是,除特定屏幕外,所有 5 个屏幕都应处于横向模式。当应用程序启动时,第一个屏幕应该是横向的,如果我进入第五个屏幕,它必须自动更改为纵向模式,这就是问题所在。
      猜你喜欢
      • 2019-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多