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

后端 未结 5 1206
花落未央
花落未央 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:17

    Using -orientation property of UIDevice is not correct (even if it could work in most of cases) and could lead to some bugs, for instance UIDeviceOrientation consider also the orientation of the device if it is face up or down, there is no pair in UIInterfaceOrientation enum for those values.
    Furthermore, if you lock your app in some particular orientation, UIDevice will give you the device orientation without taking that into account.
    On the other side iOS8 has deprecated the interfaceOrientation property on UIViewController class.
    There are 2 options available to detect the interface orientation:

    • Use the status bar orientation
    • Use size classes, on iPhone if they are not overridden they could give you a way to understand the current interface orientation

    What is still missing is a way to understand the direction of a change of interface orientation, that is very important during animations.
    In the session of WWDC 2014 "View controller advancement in iOS8" the speaker provides a solution to that problem too, using the method that replaces -will/DidRotateToInterfaceOrientation.

    Here the proposed solution partially implemented:

    -(void) viewWillTransitionToSize:(CGSize)s withTransitionCoordinator:(UIVCTC)t {
        orientation = [self orientationFromTransform: [t targetTransform]]; 
        oldOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
        [self myWillRotateToInterfaceOrientation:orientation duration: duration]; 
        [t animateAlongsideTransition:^(id <UIVCTCContext>) {
             [self myWillAnimateRotationToInterfaceOrientation:orientation
                                                      duration:duration];
          }
          completion: ^(id <UIVCTCContext>) {
             [self myDidAnimateFromInterfaceOrientation:oldOrientation];
          }];
    }
    
    0 讨论(0)
  • 2020-12-07 20:19

    Getting the device orientation is not correct. For instance, the device might be in landscape mode, but a view controller that only supports portrait will remain in portrait.

    Instead, use this:

    [[UIApplication sharedApplication] statusBarOrientation]
    

    Another bonus is that it still uses the UIInterfaceOrientation enum, so very little of your code needs to change.

    0 讨论(0)
  • 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
        }
    
    0 讨论(0)
  • 2020-12-07 20:24

    Since iOS8, Apple recommends to use TraitCollections (Size Classes) instead of interfaceOrientation.

    Moreover, since iOS 9 and the new iPad feature "Multitasking", there are some cases where the device orientation doesn't fit with the window proportions! (breaking your application UI)

    So you should also be very careful when using Regular Size Classes because it will not necessary take up the whole iPad screen.

    Sometimes TraitCollections doesn't fill all your design needs. For those cases, Apple recommends to compare view's bounds :

    if view.bounds.size.width > view.bounds.size.height {
        // ...
    }
    

    I was quite surprised, but you can check on the WWDC 2015 video Getting Started with Multitasking on iPad in iOS 9 at 21'15.

    Maybe you're really looking for the device orientation and not for the screen or window proportions. If you care about the device camera, you should not use TraitCollection, neither view bounds.

    0 讨论(0)
  • 2020-12-07 20:27

    Swift 4+ version

    UIApplication.shared.statusBarOrientation
    
    0 讨论(0)
提交回复
热议问题