如何使UIView全屏当用户将其设备从纵向旋转到横向模式时
我有一个UIView
实际上是YTPlayerView
从youtube-ios-sdk。如何使UIView全屏当用户将其设备从纵向旋转到横向模式时
当设备向左或向右旋转时,当设备旋转回肖像模式(如官方YouTube应用程序中)时,我需要将其设为全屏模式并将其恢复为非全屏模式。
我该如何实现这样的行为?
我在YTPlayerView
的界面上看不到任何setFullScreenMode
函数。
确保您试图在设备旋转时修改视图的框架?
这是我做的:
- (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'仍然有相同的大小(屏幕的一半) – FrozenHeart
看来,另一个视图阻止它(可能是因为布局约束) - http://i.imgur.com/GRn2MCz.png。我能做什么? – FrozenHeart
我在这两个设备和模拟器上都成功了。 (YTPlayerView当然有布局约束)。 你是否实现了supportedInterfaceOrientations和shouldAutorotate? 这是一个简单的视频。 https://www.youtube.com/watch?v=lyjD4V-kGOQ&feature=youtu.be –
这为我工作,在Xcode 9.2和迅速的4
var videoView: UIView!
var playerLayer: AVPlayerLayer!
if UIDeviceOrientationIsLandscape(UIDevice.current.orientation){
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
}
你有没有尝试过的视图的框架设置为UIScreen的帧大小?不知道它是否会工作 – hola