我有一个UIView
实际上YTPlayerView
来自youtube-ios-sdk。
我需要在设备向左或向右旋转时将其设为全屏,并在设备旋转回纵向模式时将其返回到非全屏模式(如在官方 YouTube 应用程序中)。
我怎样才能实现这种行为?
我在的界面中看不到任何setFullScreenMode
功能。YTPlayerView
我有一个UIView
实际上YTPlayerView
来自youtube-ios-sdk。
我需要在设备向左或向右旋转时将其设为全屏,并在设备旋转回纵向模式时将其返回到非全屏模式(如在官方 YouTube 应用程序中)。
我怎样才能实现这种行为?
我在的界面中看不到任何setFullScreenMode
功能。YTPlayerView
确保您在设备旋转时尝试修改视图的框架?
这就是我所做的:
- (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;
}
}];
}
这对我有用,在 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
}
}
确保您的设备能够旋转显示(纵向锁定应关闭)。