2017-03-08 75 views
2

searching,我怎么只能改变orientationLandScape模式spefic view controllerDevice Orientation项目settings设置为Portrait。没有得到任何成功的结果。所以最后我找到了一个办法。更改方向只针对特定视图控制器在IOS SWIFT 2.3和SWIFT 3

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

enter image description here

在SWIFT 2.3添加下面的方法

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

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


    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation { 
     return UIInterfaceOrientation.LandscapeLeft 
    } 

为SWIFT 3.0做像下面....

override var shouldAutorotate: Bool { 
     return false 
    } 

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask { 
     return .landscapeLeft 
    } 

    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { 
     return .landscapeLeft 
    } 

注意:如果您从一个视图导航到另一个视图控制器而没有导航控制器,这将工作正常。

更多的看到这个小视频教程:change device orientation for specific view controller in ios

如果任何人有更好的方式来做到这一点或意见,希望你的回答或评论在这里。 thanx。

+1

除了答案添加全局varibale - 即使你与一些自定义过渡样式模式存在,布局方向更改将无法正常工作。 –

回答

1

使用supportedInterfaceOrientationsFor应用delegate.add按照的AppDelegate代码文件

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { 

      if isIpadLandscape { 
       return UIInterfaceOrientationMask.all; 
      }else{ 
       return UIInterfaceOrientationMask.portrait; 

      } 


    } 


Add particular **viewcontroller** page you want to **rotate** 

    override func viewDidLoad() { 
      super.viewDidLoad() 
      isLandscape = true 
    } 
    override func viewWillDisappear(_ animated: Bool) { 
      isLandscape = false 
     } 

不要忘记到的appdelegate文件

import UIKit 
import CoreData 
import Foundation 
    var isLandscape = false 
    @UIApplicationMain 
相关问题