ios 仅 ONE VIEW 横向模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24928057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Only ONE VIEW landscape mode
提问by maeq
I finished my iOS app but I need to set only ONE view to landscape mode, the rest of the views can only be seen in portrait mode.
我完成了我的 iOS 应用程序,但我只需要将一个视图设置为横向模式,其余的视图只能在纵向模式下看到。
I'm using Xcode 5.1 and I created all of my Views by dropping in my storyboard View Controllers from the right panel, so if you are going to tell me to write some code somewhere, please tell me exactly where I need to write it.
我正在使用 Xcode 5.1,并且我通过从右侧面板中放入我的故事板视图控制器来创建我的所有视图,所以如果您要告诉我在某处编写一些代码,请确切地告诉我我需要在哪里编写它。
I read one solution here UINavigationController Force Rotatebut I don't know where to write that code. Do I need to create one UIViewController
manually?
我在这里阅读了UINavigationController Force Rotate 的一个解决方案,但我不知道在哪里编写该代码。我需要UIViewController
手动创建一个吗?
回答by Yaroslav
Swift
迅速
AppDelegate.swift
AppDelegate.swift
internal var shouldRotate = false
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .allButUpsideDown : .portrait
}
Your landscape view controller
您的横向视图控制器
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = true // or false to disable rotation
Objective-C
目标-C
AppDelegate.h
AppDelegate.h
@property (assign, nonatomic) BOOL shouldRotate;
@property (assign, nonatomic) BOOL shouldRotate;
AppDelegate.m
AppDelegate.m
- (UIInterfaceOrientationMask)application:(UIApplication *)application
supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return self.shouldRotate ? UIInterfaceOrientationMaskAllButUpsideDown
: UIInterfaceOrientationMaskPortrait;
}
Your landscape view controller
您的横向视图控制器
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate setShouldRotate:YES]; // or NO to disable rotation
回答by Zedenem
I am gonna suppose you are targeting iOS 7 here (using XCode 5.1, I think I am right).
我假设您在这里针对的是 iOS 7(使用 XCode 5.1,我认为我是对的)。
First, you have to understand that in order to open even just one view out of over 40 in landscape, your app should allow both landscape and portrait interface orientations.
It is the case by default, but you can check it in your target's settings, General
tab, Deployment Info
section (see screenshot below).
首先,您必须了解,为了仅打开 40 多个横向视图中的一个视图,您的应用程序应该允许横向和纵向界面方向。默认情况下是这种情况,但您可以在目标的设置、General
选项卡、Deployment Info
部分中检查它(请参见下面的屏幕截图)。
Then, because you allowed both landscape and portrait for the entire app, you will have to tell every portrait-only UIViewController
that it should not autorotate, adding this method's implementation:
然后,因为您允许整个应用程序同时使用横向和纵向,所以您必须告诉每个纵向仅UIViewController
它不应该自动旋转,添加此方法的实现:
- (BOOL)shouldAutorotate {
return NO;
}
Finally, for your specific landscape-only controller, and because you said you are presenting it modally, you can just implement these methods:
最后,对于您特定的仅限横向的控制器,并且因为您说要以模态方式呈现它,所以您可以实现以下方法:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft; // or Right of course
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
Hope this will help,
希望这会有所帮助,
回答by Bhavesh Patel
SWIFT4
SWIFT4
Add below code lines to your AppDelegate
将以下代码行添加到您的 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")
}
}
Add below code to the Controller you want to landscape
将下面的代码添加到您想要景观的控制器
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)
}
回答by Tim Le
To follow up on Yaroslav's solution, in order to allow rotation to landscape mode at only one view, the landscape view controller should have shouldRotate set to YES in its viewWillAppear method, and to NO in its viewWillDisappear.
为了跟进 Yaroslav 的解决方案,为了只允许在一个视图中旋转到横向模式,横向视图控制器应该在它的 viewWillAppear 方法中将 shouldRotate 设置为 YES,在它的 viewWillDisappear 中设置为 NO。
If you only setShouldRotate to YES at the viewWillAppear, after existing this view controller, all other view controllers will be rotated as well. So, you have to setShouldRotate to NO in its viewWillDisappear to restrict the portrait view from being rotated.
如果你只在 viewWillAppear 中将ShouldRotate 设置为 YES,那么在这个视图控制器存在之后,所有其他的视图控制器也会被旋转。因此,您必须在其 viewWillDisappear 中将ShouldRotate 设置为 NO 以限制纵向视图旋转。
回答by bleft
You should declare shouldRotate as internal to get rid of the public property in none public class warning. internal var shouldRotate = false
您应该将 shouldRotate 声明为 internal 以摆脱无公共类警告中的公共属性。 internal var shouldRotate = false