Disabling auto rotation for a UIView

前端 未结 5 1836
谎友^
谎友^ 2021-02-05 07:10

My application is composed of a toolbar and an AVCaptureVideoPreviewLayer in a background UIView. I\'d like to see that toolbar rotate regarding the device orientation, so in my

5条回答
  •  借酒劲吻你
    2021-02-05 07:35

    I ran into the same issue, though simply setting the orientation property on AVCaptureVideoPreviewLayer was not sufficient. The additional problem is that CALayer does not support layout managers on the iPhone, so the frame of the video layer may be incorrect when the device is rotated.

    I fixed this by using a custom UIView to contain the AVCaptureVideoPreviewLayer and then I overrode the layoutSubviews method for this view to ensure that the video layer's frame is always set correctly. This is also a good place to set the orientation property. That is, my UIView looks like:

    @implementation VideoPreview
    
    @synthesize previewLayer;
    
    - (void) layoutSubviews {
        if (previewLayer) {
            // set the correct orientation for the video layer
            previewLayer.orientation = [[UIDevice currentDevice] orientation];
    
            // and set its frame to that of its parent UIView
            previewLayer.frame = self.bounds;
        }
    }
    
    - (void) addPreviewLayer: (AVCaptureVideoPreviewLayer *) videoLayer {
        previewLayer = videoLayer;
        [self.layer addSublayer:videoLayer];
    }
    
    @end
    

提交回复
热议问题