AVPlayer layer inside a view does not resize when UIView frame changes

前端 未结 13 871
小蘑菇
小蘑菇 2020-12-23 17:08

I have a UIView which contains an AVPlayer to show a video. When changing orientation, I need to change the size and location of the video.

<
13条回答
  •  死守一世寂寞
    2020-12-23 17:43

    In the end I solved this by re-adding the AVPlayerLayer to the UIView. I'm not sure why changing the frame removed the layer, but it did. Here's the final solution:

    //amend the frame of the view
    [videoHolderView setFrame:CGRectMake(0, 0, 768, 502)];
    
    //reset the layer's frame, and re-add it to the view
    AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
    playerLayer.frame = videoHolderView.bounds;
    [videoHolderView.layer addSublayer:playerLayer];
    

提交回复
热议问题