“interfaceOrientation” is deprecated in iOS 8, How to change this method Objective C

后端 未结 5 1249
花落未央
花落未央 2020-12-07 19:39

I have downloaded a simpleCamera view from Cocoa Controls which use this method

- (AVCaptureVideoOrientation)orientationForConnection
{
    AVCaptureVideoOri         


        
5条回答
  •  醉话见心
    2020-12-07 20:22

    When you use UIApplication.shared.statusBarOrientation, Xcode 11 will show the warning 'statusBarOrientation' was deprecated in iOS 13.0: Use the interfaceOrientation property of the window scene instead.

    This answer is in Swift 5, and supports both iOS 13 and lower versions. It assumes the following:

    • You will create only one scene, and never more than one
    • You wish to contain your code to a specific view
    • You have a view that has a member variable previewLayer of type AVCaptureVideoPreviewLayer

    First, in your SceneDelegate.swift, add the following lines:

        static var lastCreatedScene: UIWindowScene?
    
        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            guard let newScene = (scene as? UIWindowScene) else { return }
            Self.lastCreatedScene = newScene
        }
    

    Then in your view, add the following function and call it in layoutSubviews():

        func refreshOrientation() {
            let videoOrientation: AVCaptureVideoOrientation
            let statusBarOrientation: UIInterfaceOrientation
            if #available(iOS 13, *) {
                statusBarOrientation = SceneDelegate.lastCreatedScene?.interfaceOrientation ?? .portrait
            } else {
                statusBarOrientation = UIApplication.shared.statusBarOrientation
            }
            switch statusBarOrientation {
            case .unknown:
                videoOrientation = .portrait
            case .portrait:
                videoOrientation = .portrait
            case .portraitUpsideDown:
                videoOrientation = .portraitUpsideDown
            case .landscapeLeft:
                videoOrientation = .landscapeLeft
            case .landscapeRight:
                videoOrientation = .landscapeRight
            @unknown default:
                videoOrientation = .portrait
            }
            self.previewLayer.connection?.videoOrientation = videoOrientation
        }
    

提交回复
热议问题