[英]Allow all orientations for only one view controller
I know this question was already asked in here , here and here . 我知道这个问题已经在这里 , 这里和这里被问到了。 I tried all possible ways but wasn't successful. 我尝试了所有可能的方法,但没有成功。
I'm trying to develop an application in Swift (iOS 8), in which only one view controller can be viewed in all orientations others in only portrait. 我正在尝试在Swift(iOS 8)中开发一个应用程序,其中只有一个视图控制器可以在所有方向上查看,其他只在纵向中查看。
For this, I have enabled only Portrait
mode in Target->General->Device Orientation (because I want the application to operate only in portrait mode except one view controller). 为此,我在Target-> General-> Device Orientation中仅启用了Portrait
模式(因为我希望应用程序仅在纵向模式下运行,但一个视图控制器除外)。
I have added below code to the view controller that I want to operate in all orientations: 我已将以下代码添加到我想要在所有方向上操作的视图控制器:
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.LandscapeLeft;
}
override func shouldAutorotate() -> Bool {
return true;
}
But couldn't rotate it in landscape mode. 但无法在横向模式下旋转它。
Do the following: 请执行下列操作:
in each ViewController
, override the autorotation function with either true or false 在每个ViewController
,使用true或false覆盖autorotation函数
( true
for the one that should rotate, false
for the others): (对于应旋转的那个是true
,对其他人是false
的):
override var shouldAutorotate: Bool { return true }
Hope that helps :) 希望有帮助:)
Try this in your perticular viewController which you want in all orientation, in viewWillAppear: 在viewWillAppear中,在所有方向上想要的相关viewController中尝试这个:
let value = UIInterfaceOrientationMask.All.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
and don't forget to allow orientations in Target->General->Device Orientation 并且不要忘记在Target-> General-> Device Orientation中允许方向
Give it a try 试试看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.