唯一的横屏模式视图

46

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

我使用的是Xcode 5.1,并通过从右侧面板拖放我的storyboard视图控制器来创建所有视图,因此,如果您要告诉我在哪里编写一些代码,请告诉我需要在哪里编写。

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


如果您希望在每个视图控制器中仅支持一个方向,请选择您的项目(xCode左上角的顶部项目),单击目标(您的应用程序名称),转到常规选项卡,并在设备方向中仅保留您想要应用程序处理的方向。 - Greg
也许我没有表达清楚。我有超过40个视图只应该在纵向模式下显示,还有一个视图只应该在横向模式下显示。 - maeq
你想限制方向的视图是否全部在UINavigationController中? - Greg
不,我没有使用UINavigationController。我使用按钮和模态视图来在视图之间进行切换。 - maeq
尝试在你想要限制方向的视图控制器中使用以下三种方法: -(BOOL)shouldAutorotate -(NSUInteger)supportedInterfaceOrientations
  • (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
- Greg
这可能会有所帮助 https://dev59.com/nV4b5IYBdhLWcg3wlihP - LF00
5个回答

92

Swift

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

2
你没有在AppDelegate.h中公开它,对吧? 粘贴这个: @property (assign, nonatomic) BOOL shouldRotate; 很抱歉,但是你应该学习更多这些简单的东西。我建议你去iTunes U。那里有很多视频课程,逐步教授iOS 7 开发的所有基本要素。 - Yaroslav
毫无疑问,这是最好的方法......只需让AppDelegate在每个ViewController中使用setShouldRotate:YES/NO来控制方向更改......也不需要在Info.plist或Deployment Info上设置任何内容......太棒了! - Marcos Reboucas
1
@user717452 看起来你需要强制UIViewController更新其方向。我不确定为什么它不能自动更新。我没有遇到过这个问题。 - Yaroslav
我该如何强制更新方向?设置BOOL shouldAutoRotate没有效果。 - user717452
1
糟糕的解决方案。是的,它将允许视图控制器旋转,但是如果要解除横向视图控制器并确保应用程序首先返回纵向模式,则非常麻烦。不建议采用这种方法。 - zumzum
显示剩余10条评论

39

我假设你的目标是iOS 7(使用XCode 5.1,我认为我是正确的)。

首先,您必须了解,为了在横向模式下打开40多个视图中的一个视图,您的应用程序应允许横向和纵向界面方向。 这是默认情况,但您可以在目标的设置中检查,即常规选项卡,部署信息 部分(请参见下面的屏幕截图)。

enter image description here

然后,因为您允许整个应用程序既支持横向又支持纵向,所以您将不得不告诉每个仅支持纵向的 UIViewController 不要自动旋转,并添加此方法的实现:

- (BOOL)shouldAutorotate {
  return NO;
}

最后,针对您的特定横向控制器,并且因为您表示您正在以模态方式呈现它,您只需实现这些方法:

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

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  return UIInterfaceOrientationMaskLandscape;
}

希望这能有所帮助,


这是一个解释得很清楚的答案,谢谢。但我不知道在哪里写那段代码,请问你能告诉我文件名吗? - maeq
2
嗨,如果您想让视图保持竖屏,请在所有 XXXViewController.m 文件中编写第一个代码片段。如果您想让视图保持横屏,则应将第二个代码片段放在 YYYViewController.m 文件中。如果您只是将视图控制器拖入故事板中,则应将您的竖屏视图控制器连接到至少一个 UIViewController 子类(创建/新建/文件 + UIViewController 子类),并将您的横屏视图控制器连接到不同的 UIViewController 子类。 - Zedenem
1
据我所知,即使在iOS 9.2中,@RajuGujarati仍然可以使用。请检查以下基本要素:(1)测试设备上的用户设置中是否关闭了方向锁定。也就是说,请确保其他应用程序可以成功旋转。(2)首先添加允许视图旋转的设置。创建两个视图“A”和“B”。验证两个视图都可以旋转。(3)将“autorotate NO”代码放入A的控制器中。视图A停止旋转。视图B会旋转。(4)将提到横向的两种方法放入视图B的控制器中。“B”不得有第3点中提到的代码。现在,“B”保持横向,“A”仍然保持纵向。 - ToolmakerSteve
在所有其他视图中标记shouldAutorotate为NO是一项繁琐的任务。下面给出的答案应该是正确的答案。https://dev59.com/zGAf5IYBdhLWcg3wMwM9#24928474 - Baig
但是,当我返回到上一个屏幕或推到其他屏幕时,它仍然保持横向模式? - Tà Truhoada
显示剩余2条评论

11

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)
}

6

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

如果您只在viewWillAppear中将setShouldRotate设置为YES,则退出此视图控制器后,所有其他视图控制器也会旋转。因此,您必须在viewWillDisappear中将setShouldRotate设置为NO,以限制纵向视图的旋转。


1

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接