我有一个 UIWebView 包含在 UIViewController 中,它是 UINavigationController 的后代。它看起来像这样:
该应用程序仅支持纵向。当我播放视频时,我希望用户能够旋转设备并以横向模式观看视频。我使用此代码来允许它:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
id presentedViewController = [self topMostController];
NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;
if ([className isEqualToString:@"MPInlineVideoFullscreenViewController"] ||
[className isEqualToString:@"MPMoviePlayerViewController"] ||
[className isEqualToString:@"AVFullScreenViewController"]) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
return UIInterfaceOrientationMaskPortrait;
}
- (UIViewController *)topMostController {
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
然后在我的 UINavigationController 中(所以当视频结束时, View 不会以横向呈现,而只能以纵向呈现):
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
一切正常:
但是随后视频播放完毕(或用户点击“完成”)并且屏幕返回到底层 View ,这就是发生的情况:
如您所见,导航栏滑到了状态栏下方。 此外,我在日志中收到很多自动布局错误:http://pastebin.com/09xHzmgJ
关于如何解决这个问题有什么想法吗?
最佳答案
我在 Controller 的 viewDidLoad
中使用以下代码临时解决了(通过 hack)。我必须指定代码是专门为我的情况编写的:因为我明确禁止我的 UINavigationController 横向(见上面的代码),所以当播放完成并且窗口返回纵向时,通常的通知“UIDeviceOrientationDidChange”不会被调用。但是,我希望有更好的选择,这是 SDK 的一个错误,因为它不会出现在 iOS 7 上,并且考虑到与视频播放器相关的自动布局错误数量(我无法控制) .
- (void)viewDidLoad
{
[super viewDidLoad];
// […]
/*
Hack to fix navigation bar position/height on iOS 8 after closing fullscreen video
Observe for “UIWindowDidRotateNotification” since “UIDeviceOrientationDidChangeNotification” is not called in the present conditions
Check if the notification key (“UIWindowOldOrientationUserInfoKey”) in userInfo is either 3 or 4, which means the old orientation was landscape
If so, correct the frame of the navigation bar to the proper size.
*/
[[NSNotificationCenter defaultCenter] addObserverForName:@"UIWindowDidRotateNotification" object:nil queue:nil usingBlock:^(NSNotification *note) {
if ([note.userInfo[@"UIWindowOldOrientationUserInfoKey"] intValue] >= 3) {
self.navigationController.navigationBar.frame = (CGRect){0, 0, self.view.frame.size.width, 64};
}
}];
}
然后……
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"UIWindowDidRotateNotification"];
}
关于ios - 允许使用仅纵向应用的横向视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26598848/