Ios 如何将特定视图控制器的方向锁定为纵向模式?

Ios 如何将特定视图控制器的方向锁定为纵向模式?,ios,swift,swift2,Ios,Swift,Swift2,我希望你很好 我正在使用iOS 9的Xcode开发Swift 2应用程序!我希望整个应用程序在两个方向上都能旋转,除了一个特定的视图控制器,即使在用户旋转时,我也希望它被锁定为纵向。我试了好几个小时,在网上看了很多东西,但都没用。有什么帮助吗?更新:我尝试了以下两种方法,但对iOS 9不起作用:请帮助 最好的, Anas目标C: NSNumber *orientationValue = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]

我希望你很好

我正在使用iOS 9的Xcode开发Swift 2应用程序!我希望整个应用程序在两个方向上都能旋转,除了一个特定的视图控制器,即使在用户旋转时,我也希望它被锁定为纵向。我试了好几个小时,在网上看了很多东西,但都没用。有什么帮助吗?更新:我尝试了以下两种方法,但对iOS 9不起作用:请帮助

最好的, Anas

目标C:

NSNumber *orientationValue = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:orientationValue forKey:@"orientation"];
斯威夫特:

let orientationValue = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(orientationValue, forKey: "orientation")
目标C:

NSNumber *orientationValue = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:orientationValue forKey:@"orientation"];
斯威夫特:

let orientationValue = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(orientationValue, forKey: "orientation")
试试这个:

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

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

 func onlyPortarit() -> Bool{
    if(UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait ||
        UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown ||
        UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown){
            return true
    }else{
        return false
    }
 }
试试这个:

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

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

 func onlyPortarit() -> Bool{
    if(UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait ||
        UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown ||
        UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown){
            return true
    }else{
        return false
    }
 }

除了曼努埃尔的回答。在viewDidLoad中添加以下内容


这将在纵向上初始化ViewController,而不考虑设备方向

除了曼努埃尔的回答。在viewDidLoad中添加以下内容


这将在纵向上初始化ViewController,而不考虑设备方向

可能的副本对我不起作用:请检查帮助你的副本。可能的副本对我不起作用:请检查帮助你的副本。谢谢,凯莫!但这对我不起作用,我该把代码放在哪里?我尝试了以下操作:override func ViewDidApparAnimate:Bool{super.ViewDidApparAnimated let value=UIInterfaceOrientation.Portrait.rawValue UIDevice.currentDevice.setValuevalue,forKey:orientation shouldAutorotate}只需删除shouldAutorotate并尝试覆盖func ViewDidApparanimated:Bool{super.viewDidApparanimated let value=UIInterfaceOrientation.Grait.rawValue UIDevice.currentDevice.setValuevalue,forKey:orientation}这是对Swift4当前语法的一个糟糕回答,没有解释,这不起作用@matt.writes.code已经指出了谢谢,凯莫!但这对我不起作用,我该把代码放在哪里?我尝试了以下操作:override func ViewDidApparAnimate:Bool{super.ViewDidApparAnimated let value=UIInterfaceOrientation.Portrait.rawValue UIDevice.currentDevice.setValuevalue,forKey:orientation shouldAutorotate}只需删除shouldAutorotate并尝试覆盖func ViewDidApparanimated:Bool{super.viewDidApparanimated let value=UIInterfaceOrientation.Grait.rawValue UIDevice.currentDevice.setValuevalue,forKey:orientation}这是对Swift4当前语法的一个糟糕回答,没有解释,这不符合@matt.writes.code所指出的