在iOS 8中不推荐使用"interfaceOrientation",如何更改此方法目标C.

Get*_*tMe 73 cocoa-touch objective-c ios

我从Cocoa Controls下载了一个使用此方法的simpleCamera视图

- (AVCaptureVideoOrientation)orientationForConnection
{
    AVCaptureVideoOrientation videoOrientation = AVCaptureVideoOrientationPortrait;
    switch (self.interfaceOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
            videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
            break;
        case UIInterfaceOrientationLandscapeRight:
            videoOrientation = AVCaptureVideoOrientationLandscapeRight;
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
            break;
        default:
            videoOrientation = AVCaptureVideoOrientationPortrait;
            break;
    }
    return videoOrientation;
}
Run Code Online (Sandbox Code Playgroud)

问题是在iOS 8中不推荐使用"interfaceOrientation"但我不知道在Switch条件中替换它的方法和方法.

cet*_*cet 143

获取设备方向不正确.例如,设备可能处于横向模式,但仅支持纵向的视图控制器将保留为纵向.

相反,使用这个:

[[UIApplication sharedApplication] statusBarOrientation]
Run Code Online (Sandbox Code Playgroud)

另一个好处是它仍然使用UIInterfaceOrientation枚举,因此您的代码很少需要更改.

  • 在iOS 9中不推荐使用`statusBarOrientation` - https://developer.apple.com/documentation/uikit/uiapplication/1623026-statusbarorientation?language=objc (7认同)

vme*_*yer 21

从iOS8开始,Apple建议使用TraitCollections(Size Classes)而不是interfaceOrientation.

此外,由于iOS 9和新iPad具有"多任务处理"功能,因此在某些情况下设备方向与窗口比例不符!(打破你的应用程序UI)

所以在使用常规尺寸类时你也应该非常小心,因为它不需要占用整个iPad屏幕.

有时,TraitCollections不能满足您的所有设计需求.对于这些情况,Apple建议比较视图的界限:

if view.bounds.size.width > view.bounds.size.height {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

我很惊讶,但是你可以在2015年11月15日在iOS 9上的iPad上观看WWDC 2015视频多任务入门.

也许你真的在寻找设备方向而不是屏幕或窗口比例.如果您关心设备摄像头,则不应使用TraitCollection,也不应使用视图边界.