简体   繁体   English

如果应用程序设备的方向是纵向的,那么如何仅在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

for my project, device orientation is portrair . 对于我的项目,设备方向为portrair but only for one view controller when view did load , without rotating the phone, I want to rotate the view like 'landscape left`. 但仅适用于一个视图控制器,当view did load ,不rotating手机,我想像“向左横向移动”那样旋转视图。 I tried followings . 我尝试了以下方法。 in my view did load, 我认为确实加载了

override func viewDidLoad() {
        super.viewDidLoad()

        let value = UIInterfaceOrientation.LandscapeLeft.rawValue
        UIDevice.currentDevice().setValue(value, forKey: "orientation")

    }

and I used this method ... 我用这种方法...

override func shouldAutorotate() -> Bool {
      return true
    }

but nothing happned. 但什么也没发生。 then I use following method too with above methods. 然后我也将以下方法与上述方法一起使用。

 override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.All
    }

then it cause for a crash. 那么会导致崩溃。 and nothing worked for me. 对我没有任何作用。 any idea how to do that. 任何想法如何做到这一点。

this is my project device orientation settings looks like ... 这是我的项目设备方向设置看起来像...

在此处输入图片说明

how can I do this. 我怎样才能做到这一点。 hope your help with this. 希望您对此有所帮助。

The best way to do what you want is to allow all Orientations in your app settings. 进行所需操作的最佳方法是在应用程序设置中允许所有方向

Then set the "should autorotate" variable to true il all your viewControllers 然后将所有的viewControllers 设置为“ should autorotate”变量为true

override func shouldAutorotate() -> Bool {
    return true
}

Then set the supportedInterfaceOrientations depending of your ViewContoller 然后根据您的ViewContoller设置supportedInterfaceOrientations

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .Landscape

}

or 要么

override func shouldAutorotate() -> Bool {
    return false
}

For example here I have two viewcontrollers, the view orientation will change when the function viewDidLoad will be called. 例如,这里我有两个viewcontrollers,当调用viewDidLoad函数时,视图方向将改变。

import UIKit

class PortraitViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //Here the viewController will appear in Portrait (Also all subclass of it)
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .Portrait
    }

    override func shouldAutorotate() -> Bool {
        return false
    }

}

class LandscapeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //Here the viewController will appear in Landscape (Also all subclass of it)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .Landscape

    }

    override func shouldAutorotate() -> Bool {
        return true
    }
}

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

相关问题 当应用在iOS Swift中仅支持纵向方向时,如何在我的SDK VideoViewController中强制旋转到横向方向 - How to rotate forcefully in my SDK VideoViewController to landscape orientation when app supports only portrait orientation in iOS Swift 如何设置iOS设备方向仅景观? - How to set iOS device orientation only landscape? 我将应用程序 (iOS/Swift) 的设备方向设置为仅纵向,但屏幕仍在旋转? - I have the device orientation of my app (iOS/ Swift) set to portrait only, however the screen still rotates? 如何使用UINavigationController(Swift 2.0 iOS 9)强制设置风景方向 - How to force set Landscape Orientation with UINavigationController (Swift 2.0 iOS 9) 设备方向纵向仅适用于一个视图控制器 - Device orientation portrait only for one view controller 强制纵向 IOS (swift) - Force portrait orientation IOS (swift) Swift如何在纵向设备方向内将视图转换为横向? 有可能的? - Swift How can i convert view to landscape , inside portrait device orientation? It is possible? IOS设备方向卡在纵向模式下,没有横向 - IOS Device Orientation Stuck at Portrait mode, no Landscape 在 ios 6 上仅在一个视图中启用横向 - enable landscape orientation in only one view on ios 6 仅在 ios swift 2.3 和 swift 3 中更改特定视图控制器的方向 - Change the orientation only for specific view controllers in ios swift 2.3 and swift 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM