当用户将其设备从纵向模式旋转到横向模式时,如何使UIView全屏显示

时间:2016-02-29 09:55:46

标签: ios objective-c youtube ytplayerview

UIView实际上YTPlayerView来自youtube-ios-sdk

我需要在设备向左或向右旋转时将其设为全屏,并在设备旋转回纵向模式时将其恢复为非全屏模式(如在官方YouTube应用程序中)。

我如何实现这种行为?

我在setFullScreenMode的界面中看不到任何YTPlayerView个功能。

3 个答案:

答案 0 :(得分:0)

请确保在设备旋转时尝试修改视图的框架?

这就是我的所作所为:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
     {

     } completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
     {
         UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
         switch (orientation) {
             case UIInterfaceOrientationPortrait:

                 break;
             case UIInterfaceOrientationLandscapeLeft:
             case UIInterfaceOrientationLandscapeRight:
                 [self.testView setFrame:CGRectMake(0, 0, size.width, size.height)];
                 break;
             default:
                 break;
         }
     }];
}

答案 1 :(得分:0)

确保您的设备能够旋转显示(纵向锁定应该关闭)。

答案 2 :(得分:0)

这对我有用,在xcode 9.2和swift 4

var videoView: UIView!
var playerLayer: AVPlayerLayer!

override func viewDidLayoutSubviews(){
    if UIDeviceOrientationIsLandscape(UIDevice.current.orientation) && !(UIDevice.current.orientation.isFlat) {
        let screenSize = UIScreen.main.bounds
        let screenWidth = screenSize.width
        let screenHeight = screenSize.height
        videoView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)
        playerLayer.frame = videoView.bounds
    }
}