【问题标题】:iPhone lock portrait orientationiPhone 锁定纵向
【发布时间】:2014-04-02 01:02:00
【问题描述】:

我意识到关于这个问题有几个线程。但是,我对此并不陌生,我发现的一些解决方案对我不起作用。

我有一个 iPhone 应用程序,它应该支持除一个视图之外的所有方向。这个视图我只想锁定纵向。我试过这段代码,但是当我翻转手机时它仍然进入横向模式......我做错了什么?

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

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

【问题讨论】:

    标签: ios orientation


    【解决方案1】:

    注意:shouldAutorotateToInterfaceOrientation 在 iOS 6.0 中一直是 deprecated

    您现在应该覆盖 supportedInterfaceOrientationpreferredInterfaceOrientationForPresentation 类。

    像这样:

    - (NSUInteger) supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskPortrait;
    }
    
    - (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
        return UIInterfaceOrientationPortrait;
    }
    

    【讨论】:

    • 我尝试了上面的代码,但是当我翻转手机时,视图仍然进入横向模式......还有什么我需要做的吗?谢谢!
    • 您是否从代码中删除了 shouldAutorotateToInterfaceOrientation 类?
    • +1 - 有趣,当您添加评论时,我正在阅读这篇文章。本来就是这样!
    【解决方案2】:

    底线是,如果您使用的是 UINavigationController,它通常不会将supportedInterfaceOrientations 调用转发到顶视图控制器。子类 UINavigationController 并添加:

    -(NSUInteger)supportedInterfaceOrientations
    {
        return [self.topViewController supportedInterfaceOrientations];
    }
    

    似乎是最干净的答案。还有其他方法涉及将代码放入您的应用程序委托,使用类别将代码添加到 UINavigationController 等,但这对我来说似乎是最干净的解决方案。

    【讨论】:

    • 感谢您的回答。我尝试了代码,但收到一条错误消息,指出“找不到属性 topViewController...”
    • 您需要将此代码放入 UINavigationController 子类中,然后确保您的故事板引用该子类。
    【解决方案3】:

    您必须将控制器锁定在您的 RootViewController(eg.UITabbarController 或 UINavigationController) 中。当你的设备旋转时,首先它会调用AppDelegate的rotateMethod(eg.- (BOOL)shouldAutorotate , - (NSUInteger)supportedInterfaceOrientations)。然后它会调用RootViewController的RotateMehod。

    1.如果您的 RootViewController 是 UITabbarController,请在您的 UITabbarController 中添加类似以下代码的代码(NotRotateController 是您不想进入横向模式的控制器)

    #import "NotRotateController.h"
    - (BOOL)shouldAutorotate
    {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        UINavigationController * baseNavCtr = self.viewControllers[self.selectedIndex];
        if ([baseNavCtr.topViewController isKindOfClass:[NotRotateController class]]) {
            return UIInterfaceOrientationMaskPortrait;
        }else {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    }
    

    2.如果你的 RootViewController 是 UINavigationContoller

    #import "NotRotateController.h"
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        if (self.topViewController isKindOfClass:[NotRotateController class]) {
            return UIInterfaceOrientationMaskPortrait;
        }else {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    }
    
    - (BOOL)shouldAutorotate {
        return YES;
    }
    

    3.总结,在RootViewController的- (NSUInteger)supportedInterfaceOrientations方法中判断你的Controller,返回你想要支持的方向

    【讨论】:

      【解决方案4】:

      试试这个:

      1- 将这 2 个函数添加到 App Delegate。阅读 cmets 了解更多信息

      class AppDelegate: UIResponder, UIApplicationDelegate {
      
          // 1.
          func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
      
              if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
      
                  if (rootViewController.responds(to: Selector(("canRotate")))) {
                      // Unlock landscape view orientations for this view controller
                      return .allButUpsideDown
                  }
      
                  // *** THIS WILL GET CALLED WHEN THE VC CALLS THIS SELECTOR ***
                  if (rootViewController.responds(to: Selector(("cantRotate")))) {
                      // Unlock portrait only view orientation for this view controller
                      return .portrait
                  }
              }
      
              // Allow all orientations but upside down
              return .allButUpsideDown
          }
      
          // 2.
          private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
              if (rootViewController == nil) { return nil }
              if (rootViewController.isKind(of: UITabBarController.self)) {
                  return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController)
              } else if (rootViewController.isKind(of: UINavigationController.self)) {
                  return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController)
              } else if (rootViewController.presentedViewController != nil) {
                  return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController)
              }
              return rootViewController
          }
      }
      

      2- 将此添加到您只想成为肖像的 vc 中:

      class YourViewController: UIViewController {
      
          // MARK: - Portrait Only. The first function above inside App Delegate responds to this
          @objc func cantRotate() {}
      }
      

      【讨论】:

        猜你喜欢
        • 2011-04-11
        • 1970-01-01
        • 1970-01-01
        • 2019-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多