【问题标题】:How to make UIView fullscreen when user rotates its device from portrait to landscape mode当用户将其设备从纵向模式旋转到横向模式时如何使 UIView 全屏显示
【发布时间】:2016-06-12 07:30:34
【问题描述】:

我有一个UIView,实际上是来自youtube-ios-sdkYTPlayerView

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

我怎样才能实现这样的行为?

我在YTPlayerView 的界面中没有看到任何setFullScreenMode 函数。

【问题讨论】:

  • 您是否尝试将视图的框架设置为 UIScreen 的框架大小?不确定它是否会起作用

标签: ios objective-c youtube ytplayerview


【解决方案1】:

这对我有用,在 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
    }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我正在模拟器上测试我的应用程序,所以是的,它可以旋转显示
    【解决方案3】:

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

    这就是我的工作:

    - (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;
             }
         }];
    }
    

    【讨论】:

    • 感谢您的回答,但不幸的是它不起作用 -- YTPlayerView 仍然具有相同的大小(屏幕的一半)
    • 似乎另一个视图阻止了它(可能是因为布局限制)——i.imgur.com/GRn2MCz.png。我能做什么?
    • 我在设备和模拟器上都成功了。 (YTPlayerView 当然有布局约束)。您是否实现了 supportedInterfaceOrientations 和 shouldAutorotate ?这是一个刚刚制作的简单视频。 youtube.com/watch?v=lyjD4V-kGOQ&feature=youtu.be
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多