【问题标题】:Only ONE VIEW landscape mode只有 ONE VIEW 横向模式
【发布时间】:2014-09-15 16:11:39
【问题描述】:

我完成了我的 iOS 应用,但我只需要将一个视图设置为横向模式,其余视图只能在纵向模式下查看。

我使用的是 Xcode 5.1,我通过从右侧面板中放入我的故事板视图控制器来创建我所有的视图,所以如果你要告诉我在某处写一些代码,请告诉我我需要在哪里写写吧。

我在这里阅读了一个解决方案UINavigationController Force Rotate,但我不知道在哪里编写该代码。我需要手动创建一个UIViewController吗?

【问题讨论】:

  • 如果您想在每个视图控制器中仅支持一个方向,请选择您的项目(xCode 左上角的顶部项目)单击目标(​​您的应用程序名称)转到“常规”选项卡并在“设备方向”中仅选中您希望应用处理的方向。
  • 也许我没有说清楚。我有 40 多个只能在纵向模式下看到的视图,而我有一个只能在横向模式下看到的视图。
  • UINavigationController 中的所有视图(您想限制方向)吗?
  • 不,我没有使用 UINavigationController。我使用按钮和模式从一个视图移动到另一个视图。
  • 尝试使用所有三种方法:-(BOOL)shouldAutorotate -(NSUInteger)supportedInterfaceOrientations 和 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 在您想要限制方向的视图控制器中。

标签: ios objective-c xcode landscape viewcontroller


【解决方案1】:

SWIFT4

将以下代码行添加到您的 AppDelegate

var orientationLock = UIInterfaceOrientationMask.portrait
        
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
  return self.orientationLock
}
        
struct AppUtility {
  static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
    if let delegate = UIApplication.shared.delegate as? AppDelegate {
      delegate.orientationLock = orientation
    }
  }
            
  static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
    self.lockOrientation(orientation)
    UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
  }
}

将以下代码添加到您要横向的控制器中

override func viewDidLoad() {
  super.viewDidLoad()
}
        
override func viewWillAppear(_ animated: Bool) {
  AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.landscapeRight, andRotateTo: UIInterfaceOrientation.landscapeRight)
}
    
override func viewWillDisappear(_ animated : Bool) {
  super.viewWillDisappear(animated)
  AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo: UIInterfaceOrientation.portrait)
}

【讨论】:

    【解决方案2】:

    斯威夫特

    AppDelegate.swift

    internal var shouldRotate = false
    func application(_ application: UIApplication,
                     supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return shouldRotate ? .allButUpsideDown : .portrait
    }
    

    您的横向视图控制器

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.shouldRotate = true // or false to disable rotation
    

    Objective-C

    AppDelegate.h

    @property (assign, nonatomic) BOOL shouldRotate;

    AppDelegate.m

    - (UIInterfaceOrientationMask)application:(UIApplication *)application
     supportedInterfaceOrientationsForWindow:(UIWindow *)window {
        return self.shouldRotate ? UIInterfaceOrientationMaskAllButUpsideDown
                                 : UIInterfaceOrientationMaskPortrait;
    }
    

    您的横向视图控制器

    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [appDelegate setShouldRotate:YES]; // or NO to disable rotation
    

    【讨论】:

    • 你能告诉我我添加到我的故事板的视图控制器的代码在哪里吗?
    • 如果我没听错的话,这应该会帮助stackoverflow.com/questions/11372208/…
    • 您没有在 AppDelegate.h 中公开它,是吗?粘贴:@property (assign, nonatomic) BOOL shouldRotate;对不起,但你应该了解更多这些简单的东西。我可以建议你去 iTunes U。有很多视频课程一步步教你关于 iOS 7 开发的所有基本知识。
    • @user717452 看来您需要强制 UIViewController 更新其方向。我不知道为什么它不自己做。我没有遇到过这个问题。
    • 糟糕的解决方案。是的,它将允许视图控制器旋转,但关闭景观视图控制器并确保应用程序在繁琐时首先返回纵向模式。不推荐这种方法。
    【解决方案3】:

    为了跟进 Yaroslav 的解决方案,为了只允许在一个视图中旋转到横向模式,横向视图控制器应该在其 viewWillAppear 方法中将 shouldRotate 设置为 YES,并在其 viewWillDisappear 中设置为 NO。

    如果你只在 viewWillAppear 处将ShouldRotate 设置为 YES,那么在存在这个视图控制器之后,所有其他的视图控制器也会被旋转。因此,您必须在其 viewWillDisappear 中将ShouldRotate 设置为 NO 以限制纵向视图被旋转。

    【讨论】:

      【解决方案4】:

      您应该将 shouldRotate 声明为 internal 以摆脱非公共类警告中的公共属性。 internal var shouldRotate = false

      【讨论】:

        【解决方案5】:

        我假设您在这里定位 iOS 7(使用 XCode 5.1,我想我是对的)。

        首先,您必须了解,即使只打开 40 多个横向视图中的一个,您的应用也应该允许横向和纵向界面方向。 默认情况下是这种情况,但您可以在目标的设置、General 选项卡、Deployment Info 部分中进行检查(参见下面的屏幕截图)。

        然后,由于您允许整个应用程序同时使用横向和纵向,您将不得不告诉每个仅纵向的UIViewController 它不应自动旋转,并添加此方法的实现:

        - (BOOL)shouldAutorotate {
          return NO;
        }
        

        最后,对于您的特定横向控制器,并且因为您说您是以模态方式呈现它,您可以实现这些方法:

        - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
          return UIInterfaceOrientationLandscapeLeft; // or Right of course
        }
        
        - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
          return UIInterfaceOrientationMaskLandscape;
        }
        

        希望这会有所帮助,

        【讨论】:

        • 这是一个很好解释的答案,谢谢。但是我不知道在哪里写那个代码,你能告诉我文件名吗?
        • 您好,您应该在您希望视图保持纵向的所有XXXViewController.m 文件中编写第一个代码sn-p。第二个位于您希望视图保持横向的YYYViewController.m 文件中。如果您只是将视图控制器拖到情节提要中,则应将纵向视图控制器连接到至少一个 UIViewController 子类(创建/新建/文件 + UIViewController 的子类),将横向视图控制器连接到 UIViewController 的不同子类。
        • 如果您无法理解我之前的评论,我强烈建议您学习基本的 iOS 开发教程,例如:developer.apple.com/library/ios/referencelibrary/GettingStarted/…
        • 谢谢,我将创建两个 UIViewController 子类并添加您提供的代码。非常感谢您的帮助
        • @RajuGujarati AFAIK,即使在 ios 9.2 中仍然有效。检查所有基础知识:(1)在用户设置中没有打开方向锁定的设备上进行测试。也就是说,确保其他应用程序成功旋转。 (2) 首先只需添加允许视图旋转的设置。创建两个视图“A”和“B”。验证两个视图都在旋转。 (3) 在 A 的控制器中输入“autorotate NO”代码。 Vew A 停止旋转。视图 B 确实旋转。 (4) 把提到风景的两个方法放到View B的控制器中。 “B”不能有(3)中提到的代码。 “B”现在保持横向。 “A”仍然是纵向的。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-10
        相关资源
        最近更新 更多