更改设备方向,swift,iOS,

时间:2016-05-11 13:01:01

标签: ios swift

如何使用代码将设备方向更改为纵向?

从视图控制器以纵向打开相机和库。

我想在此按钮中加入:

  

@IBAction func openCamera(sender:AnyObject){/// accion del   博特恩卡马拉

    // Create image picker controller


    let image = UIImagePickerController()

    image.delegate = self

    image.sourceType = UIImagePickerControllerSourceType.Camera
    image.cameraDevice  = UIImagePickerControllerCameraDevice.Front
    self.presentViewController(image, animated: true, completion: nil)
}

我收到了这个错误:

  

由于未捕获的异常而终止应用   ' UIApplicationInvalidInterfaceOrientation',原因:'支持   方向与应用程序没有共同的方向,并且   [PLUICameraViewController shouldAutorotate]返回YES'   ***首先抛出调用堆栈:(0x18150ee38 0x180b73f80 0x18150ed80 0x1866d1af0 0x1866db150 0x1866db0c8 0x1866d9bb0 0x186653910   0x1866536ac 0x1866536ac 0x1866536ac 0x186652c40 0x181e40d50   0x186652ac4 0x186660578 0x18675a11c 0x186759a40 0x1869b8740   0x18690afd8 0x186918990 0x18664a4a4 0x1814c47b0 0x1814c2554   0x1814c2984 0x1813ecd10 0x182cd4088 0x1866c1f70 0x1000675ac   0x180f8a8b8)libc ++ abi.dylib:以未捕获的异常终止   输入NSException(lldb)

当我使用

> presentViewController(cameraViewController, animated: true,
> completion: nil)
>     }
>     */
>     
>         // Create image picker controller
>         
>         
>         let image = UIImagePickerController()
>         
>         image.delegate = self
>         
>         image.sourceType = UIImagePickerControllerSourceType.Camera
>         image.cameraDevice  = UIImagePickerControllerCameraDevice.Front
>         self.presentViewController(image, animated: true, completion: nil)
>     }

3 个答案:

答案 0 :(得分:0)

要以编程方式更改方向,请使用:

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

答案 1 :(得分:0)

强制设备方向不是要走的路。 问题也不是设备而是界面方向:)阅读有关

的区别

但要解决此问题,只需在您的应用中允许纵向模式,并在AppDelegate或您应用的播放器中指定。 :)

//allow rotation for app
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
    return UIInterfaceOrientationMaskAll;
}

... 你可以然后禁止它用于某些VC而不是一切 ...

//for a vc
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeLeft;
}
- (BOOL)shouldAutorotate {
    return YES;
}

答案 2 :(得分:0)

使用setter方法设置方向

  private var orientations = UIInterfaceOrientationMask.landscapeLeft
    override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
        get { return self.orientations }
        set { self.orientations = newValue }
    }