【问题标题】:How can I lock orientation for a specific view an Objective-C iPhone app in iOS 8?如何在 iOS 8 中锁定特定视图的 Objective-C iPhone 应用程序的方向?
【发布时间】:2015-08-03 18:30:58
【问题描述】:

我只是想设置我的应用,以便在横向模式下只能查看一个视图。

我尝试了从shouldAutorotatesupportedInterfaceOrientationpreferedInterfaceOrientationForPresentation 的几乎所有方法,并将[UIDevice currentDevice] 的方向设置为纵向。我在 Info.plist 和项目的常规部分中启用了 Landscape。

【问题讨论】:

    标签: ios objective-c orientation


    【解决方案1】:

    首先让你的应用画像只在你的info.plist中

    在您的 AppDelegate 类中创建以下属性:

    @property BOOL restrictRotation;
    

    然后在您的 AppDelegate.m 类中创建以下方法:

    -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        if(self.restrictRotation)
            return UIInterfaceOrientationMaskPortrait;
        else
            return UIInterfaceOrientationMaskAll;
    }
    

    在您的 ViewController 中创建以下方法,并在您想要允许横向显示之前调用它。 (首先在 viewDidLoad 方法中调用它以确保限制旋转)

    -(void) restrictRotation:(BOOL) restriction
    {
        AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        appDelegate.restrictRotation = restriction;
    }
    

    像这样:

    [self restrictRotation:NO];
    

    在完成横向视图并将其关闭后,立即调用:

    [self restrictRotation:YES];
    

    希望这能回答你的问题。

    【讨论】:

    • 这对我有用。 =) 我还想在部署信息中添加检查“需要全屏”为我节省了很多时间。特别是对于嵌入在 Master Detail View Controller 或 UINavigation Controllers 中的 iOS 应用程序。
    【解决方案2】:

    锁定后也改变方向:

    -(void)forceOrientation:(UIInterfaceOrientation)orientation{
        [[UIDevice currentDevice]setValue:[NSNumber numberWithInteger:orientation]forKey:@"orientation"];
    }
    
    • 在应用委托中设置

    【讨论】:

      【解决方案3】:

      对于 iOS 6 及更高版本,要在使用 Gurtej Singh 答案时清除 AppDelegate 中的警告,您可以将 AppDelegate 代码替换为:

          - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window  API_AVAILABLE(ios(6.0)) API_UNAVAILABLE(tvos)
      {
          if(self.restrictRotation)
              return UIInterfaceOrientationMaskPortrait;
          else
              return UIInterfaceOrientationMaskAll;
      }
      

      【讨论】:

        【解决方案4】:

        在您的应用委托中,您可以这样做:

        // Used to show a video in full screen
        func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int
        {
            if let presentedViewController = self.window?.rootViewController?.presentedViewController
            {
                if !presentedViewController.isBeingDismissed() && presentedViewController is MyLandscapeModeViewController // insert your view controller class name here
                {
                    // specify what kind of Orientation you want
                    return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
                }
            }
            // show every other View Controller in portrait
            return Int(UIInterfaceOrientationMask.Portrait.rawValue)
        }
        

        【讨论】:

          猜你喜欢
          • 2015-05-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-07
          • 2013-12-06
          • 1970-01-01
          • 2014-08-19
          • 1970-01-01
          相关资源
          最近更新 更多