简体   繁体   English

强制横向,Swift

[英]Force Landscape orientation, Swift

I have a view controller that is designed in portrait view.我有一个在纵向视图中设计的视图控制器。 I have another view controller designed in Landscape view (in storyboards).我有另一个在横向视图中设计的视图控制器(在故事板中)。 On my first view controller (portrait VC) i have a button that takes you to the landscape VC.在我的第一个视图控制器(纵向 VC)上,我有一个按钮可以将您带到横向 VC。 When you click on the button the Landscape view controller shows up in portrait view initially and doesn't change until rotated.当您单击按钮时,横向视图控制器最初显示在纵向视图中,并且在旋转之前不会改变。 I would like to have this landscape view controller automatically show up in landscape without having to rotate it.我想让这个横向视图控制器自动显示在横向中,而无需旋转它。 Ive been coding this in swift, and have had no luck.我一直在快速编码,但没有运气。 In my plist menus i have all orientations enabled.在我的 plist 菜单中,我启用了所有方向。 Any help would be great.任何帮助都会很棒。

You should add this in the second viewcontroller (designed to be in landscape) in the viewDidLoad您应该在viewDidLoadsecond viewcontroller (设计为横向)中添加它

It is in swift3 :它在swift3

let value =  UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()

Hope it will help you :)希望它会帮助你:)

I'm working on a similar scenario I want to load and keep my first ViewController in Landscape orientation while allowing subviews to switch as needed我正在处理一个类似的场景,我想加载并保持我的第一个 ViewController 处于横向,同时允许子视图根据需要切换

I think placing我认为放置

override func viewDidLoad() {
    super.viewDidLoad()
    let value = UIInterfaceOrientation.LandscapeLeft.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

Swift 4斯威夫特 4

 override func viewDidLoad() {
        super.viewDidLoad()
        let value = UIInterfaceOrientation.landscapeLeft.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
    }

should load your view in landscape mode...But if holding phone in portrait orientation it will soon rotate...and that's the part I'm at...How to prevent the rotation.应该在横向模式下加载您的视图......但是如果以纵向方式拿着手机它很快就会旋转......这就是我所在的部分......如何防止旋转。

I believe the best and most robust way to achieve this is by overriding two attributes - and avoid force orientation changes of the windows (as suggested by others).我相信实现这一目标的最好和最强大的方法是覆盖两个属性 - 并避免窗口的力方向变化(如其他人所建议的那样)。

This is how I got one of my view controllers to always show up in landscape mode, and stay there, without affecting the entire navigation stack.这就是我如何让我的一个视图控制器始终以横向模式显示并保持在那里,而不影响整个导航堆栈。 Add both of these overrides to the landscape view controller:将这两个覆盖添加到横向视图控制器:

override var shouldAutorotate: Bool {
    false
}

This will prevent the view controller from auto-rotating when device orientation changes.这将防止视图控制器在设备方向改变时自动旋转。 Now that the view controller no longer rotates by itself, all you need to do is request the orientation you want:现在视图控制器不再自行旋转,您需要做的就是请求您想要的方向:

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    UIInterfaceOrientation.landscapeLeft
}

And voila!瞧! Should do what you want.应该做你想做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 强制UIWebView横向放置? - Force landscape orientation for UIWebView? 在UIImagePickerController中强制横向方向 - Force landscape orientation in UIImagePickerController 如何使用UINavigationController(Swift 2.0 iOS 9)强制设置风景方向 - How to force set Landscape Orientation with UINavigationController (Swift 2.0 iOS 9) 方向锁定时强制横向 - Force Landscape when orientation is locked 在一个视图中强制横向 - Force landscape orientation in one View Swift Webview力图 - Swift Webview force landscape Swift 中集合视图的横向方向 - Landscape orientation for Collection View in Swift 如果应用程序设备的方向是纵向的,那么如何仅在iOS中为一个视图构建者设置强制横向,Swift 2.3 - if app device orientation is portrait how to set force landscape only for one view constroller in ios, swift 2.3 如何强制Swift iOS游戏应用程序以横向打开? (GameViewController) - How to force Swift iOS game application to open up in landscape orientation? (GameViewController) 在一个视图控制器中强制横向定位后重置Xcode中的自动旋转(Swift 4) - Reset autorotation in Xcode after force landscape orientation in one view controller (swift 4)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM