2017-03-07 49 views
0

为我的项目,设备方向是portrair。但只有一个视图控制器,当view did load,没有rotating电话,我想旋转视图像'风景左'。我尝试了以下。在我看来没有负荷,如果应用程序的设备方向是纵向如何设置强制景观只有一个视图Constroller在ios,swift 2.3

override func viewDidLoad() { 
     super.viewDidLoad() 

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

    } 

,我用这个方法...

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

但没有happned。然后我用上面的方法也使用下面的方法。

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

然后它会导致崩溃。并没有为我工作。任何想法如何做到这一点。

这是我的项目设备方向设置看起来像......

enter image description here

我怎么能做到这一点。希望你对此有所帮助。

+0

请查看http://www.jairobjunior.com/blog/2016/03/05/how-to-rotate-only-one-view-controller-to-landscape-in​​-ios-slash- swift/ –

+0

尝试'AppDelegate'中的'func application(_ application:UIApplication,supportedInterfaceOrientationsFor window:UIWindow?) - > UIInterfaceOrientationMask',然后检查你的控制器是否可见 – zombie

回答

0

做你想做的事情的最好方法是在你的应用程序设置中使用allow all Orientations

然后设置“应自转”变量设置为true IL所有viewControllers

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

然后设置supportedInterfaceOrientations根据您的ViewContoller的

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

} 

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

例如,在这里我有两个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 
    } 
} 
相关问题