我的应用程序是一个仅限肖像的应用程序,但我有一个视图控制器,它使用AVPlayerViewController显示实时流。
为了允许横向播放该播放器的全屏视图,我在 AppDelegate.swift 中编写了此方法:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
var orientation = UIInterfaceOrientationMask.Portrait
if let presentedController = window?.rootViewController?.presentedViewController? {
if presentedController.isKindOfClass( NSClassFromString("AVFullScreenViewController").self ) {
orientation = .AllButUpsideDown
} else if let navController = presentedController as? UINavigationController {
if navController.topViewController.isKindOfClass( NSClassFromString("AVFullScreenViewController").self ) {
orientation = .AllButUpsideDown
}
}
}
return Int(orientation.rawValue)
}
这就是我调用初始化我的AVPlayer的方法:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showLiveStream" {
SVProgressHUD.show()
var queue: dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(queue, {
let streamURL = NSURL(string: liveStreamURL)
let playerItem = AVPlayerItem(URL: streamURL)
let player = AVPlayer(playerItem: playerItem)
dispatch_async(dispatch_get_main_queue(), {
SVProgressHUD.dismiss()
var playerViewController = segue.destinationViewController as AVPlayerViewController
playerViewController.player = player
})
})
}
}
问题:当我打开播放器的全屏视图时,然后切换到横向,然后点击"完成"要关闭全屏视图,我的应用程序保持在横向。但我想让它再次旋转成肖像。我怎么能这样做?
答案 0 :(得分:0)
而不是实现application(_:supportedInterfaceOrientationsForWindow:)
尝试在每个视图控制器上实现supportedInterfaceOrientations()
。例如:
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
这将确保视图控制器无法以横向显示,因此在关闭视频播放器时,它将直接返回纵向窗口。
更新目标C:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
答案 1 :(得分:0)
在Appdelegate和您的必需视图控制器中添加这些行
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
//LandScapeMode:- UIInterfaceOrientationMaskLandscape;
//PortraitMode:-
return UIInterfaceOrientationMaskPortrait
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
//LandScapeMode:- UIInterfaceOrientationLandscapeRight;
// ProtraitMode:-
return UIInterfaceOrientationPortrait
}
答案 2 :(得分:0)
我为解决此问题所做的工作,创建了一个包含' viewDidLayoutSubviews'功能并覆盖它!
final class NewMoviePlayerViewController: AVPlayerViewController {
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if view.bounds == contentOverlayView?.bounds {
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
}
}