[英]Change device orientation, swift, iOS,
如何使用代码将设备方向更改为“纵向”?
为了从视图控制器中以纵向打开相机和库。
我想在此按钮中添加:
@IBAction func openCamera(sender:AnyObject){/// accion del boton Camara
// 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 0x1866db150186 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)
> }
为了以编程方式更改方向,请使用以下命令:
let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
强制设备定位不是要走的路。 问题也不是设备,而是接口方向:)有一个区别需要阅读
但要解决此问题,只需在您的应用中允许使用纵向模式,并在AppDelegate或应用的plist中指定即可。 :)
//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;
}
使用setter方法设置方向
private var orientations = UIInterfaceOrientationMask.landscapeLeft
override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
get { return self.orientations }
set { self.orientations = newValue }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.