【问题标题】:change one view orientation to landscape将一种视图方向更改为横向
【发布时间】:2014-06-02 21:31:45
【问题描述】:

我正在开发一个用户将观看直播频道的项目。但是我在这里面临一个小问题,我已经非常努力但未能找到任何解决方案。我的应用程序将支持所有视图的纵向方向,但最后一个视图。最后一个视图将仅支持横向。可能吗? 我已经搜索并尝试了以下代码

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];

//

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

//

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation        {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);}

【问题讨论】:

    标签: ios objective-c device-orientation


    【解决方案1】:

    关注this link。希望您能在这里得到答案。

    该链接显示了如何将所有视图保持为纵向模式,除了一个将是横向的视图。

    您需要执行以下操作:

    第一:

    在所有修复纵向的控制器中实现这一点:

    - (BOOL)shouldAutorotate
    {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    

    并为景观控制器实现这一点:

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

    第二次:

     // Fetch the status bar from the app and set its orientation as required. 
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
    
    // Create an empty view controller, present it modally and remove it directly afterwards
    UIViewController *mVC = [[UIViewController alloc] init];
    [self presentModalViewController:mVC animated:NO];
    [self dismissModalViewControllerAnimated:NO];
    // Now the device is rotated to the desired orientation. Go from there.
    

    希望有效。

    【讨论】:

    • 我已经实现了,但它也旋转到其他方向,我不想自动旋转
    • @AltafBangash 好的,所以你不想自动旋转。然后请说明您的确切要求。这就是我从您的问题中得到的:您希望所有视图都处于纵向模式,除了最后一个视图 - 您只希望在横向模式下。如果我理解错了,请纠正我。
    • 但是你的代码允许两个方向(纵向和横向),我只想要一个
    • 但是你说你想要所有的纵向视图,除了最后一个是横向的,对吧?请再次说明您的要求:)
    【解决方案2】:

    从 iOS6 苹果改变了方向更新机制。从 iOS6 开始,iOS 将仅向 RootController 报告方向事件,因此所有与方向相关的决定只能在 RootController 中进行。假设您使用 UINavigationController 或 UITabBarController 作为窗口的根控制器。在这种情况下,您可以创建 UINavigationController 或 UITabBarController 的子类,覆盖方向相关的方法并将方向相关的事件传递给 childControllers。将此自定义 UINavigationController 或 UITabBarController 对象设置为窗口的 rootController。

    您可以在自定义类中覆盖以下方法。

    -(BOOL)shouldAutorotate
    {
        BOOL shouldRotate = YES;
        if(self.viewControllers.count > 0)
            shouldRotate = [[self.viewControllers lastObject] shouldAutorotate];
        return shouldRotate;
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
        NSUInteger supportedInterfaces = UIInterfaceOrientationMaskAll;
        if(self.viewControllers.count > 0)
            supportedInterfaces = [[self.viewControllers lastObject] supportedInterfaceOrientations];
        return supportedInterfaces;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        UIInterfaceOrientation preferredOrientation = UIInterfaceOrientationPortrait;
        if(self.viewControllers.count > 0)
            preferredOrientation = [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
        return preferredOrientation;
    }
    

    【讨论】:

      【解决方案3】:

      使用以下方法:在您的应用委托.h

      @interface PlayWithWSWithLibAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
      
             BOOL flagOrientationAll;
      }
      
      @property (assign) BOOL flagOrientationAll;
      

      在您的应用委托 .m 文件中添加以下方法

      - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
          //NSLog(@"PlayWithWSWithLibAppDelegate -- supportedInterfaceOrientationsForWindow");
          if(flagOrientationAll == YES){
              return UIInterfaceOrientationMaskLandscapeRight;
          } else {
              return UIInterfaceOrientationMaskPortrait ;  // your Default orientation for all other view
          }
       }
      

      在您想要为 iPhone 设备纵向和横向旋转的视图中实现以下方式

      -(void)viewWillAppear:(BOOL)animated
      {
          self.tabBarController.delegate = self;
      
          PlayWithWSWithLibAppDelegate *delegate = (PlayWithWSWithLibAppDelegate *) [[UIApplication sharedApplication] delegate];
          delegate.flagOrientationAll = YES;
       }
      }
      
      -(void)viewWillDisappear:(BOOL)animated
      {
          //NSLog(@"viewWillDisappear -- Start");
           PlayWithWSWithLibAppDelegate *delegate = (PlayWithWSWithLibAppDelegate *)[[UIApplication sharedApplication] delegate];
              delegate.flagOrientationAll = NO;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多