【问题标题】:Change device orientation, swift, iOS,更改设备方向,swift,iOS,
【发布时间】:2016-05-11 13:01:01
【问题描述】:

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

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

我想在这个按钮中加入:

@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)
>     }

【问题讨论】:

  • 强制设备方向不是要走的路。问题也不是设备,而是界面方向以及您在 AppDelegate 或应用程序的 plist 中指定的。你禁止人像吗?然后允许它:P
  • 这确实意味着您的视图控制器必须支持它。但它将为需要它的 VC 启用它

标签: ios swift


【解决方案1】:

为了以编程方式改变方向,使用这个:

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

【讨论】:

  • 我不认为这是苹果批准的 ;) 错误是界面方向。不是设备方向本身
【解决方案2】:

强制设备方向不是要走的路。 问题也不是设备,而是界面方向:) 有一个区别需要阅读

但要解决此问题,只需在您的应用中允许纵向模式并在 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;
}

【讨论】:

    【解决方案3】:

    使用setter方法设置方向

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

    【讨论】:

      猜你喜欢
      • 2014-10-28
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      • 1970-01-01
      相关资源
      最近更新 更多