如何旋转嵌入在UIWebView中的视频(仅适用于iOS 7)?

时间:2014-02-28 13:03:00

标签: objective-c video ios7 uiwebview landscape

我正在开发的应用程序是面向纵向的,但是当视频正在运行时(它嵌入在webview中),我需要以横向模式重新定位视频。我该怎么办?我找到了一个解决方案,直到几天前工作得很好:)。我相信这是因为iOS 7更新,但我不确定。所以,这就是我之前使用的,但它不再起作用了,因为窗口和类名总是零。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{

id presentedViewController = [window.rootViewController presentedViewController];
NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;

if (window && [className isEqualToString:@"MPInlineVideoFullscreenViewController"]) {
    return UIInterfaceOrientationMaskAll;
} else {
    return UIInterfaceOrientationMaskPortrait;
}

8 个答案:

答案 0 :(得分:9)

我终于找到了自己的解决方案!我在AppDelegate中实现了以下方法,并且它有效。我的问题是,起初,我没有检查正确的视图控制器。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

NSString *className = NSStringFromClass([window class]);
if ([((UINavigationController *)window.rootViewController) respondsToSelector:@selector(visibleViewController)]) {
    className = NSStringFromClass([((UINavigationController *)window.rootViewController).visibleViewController class]);
}

if ([className isEqualToString:@"MPFullscreenWindow"] || [className isEqualToString:@"MPInlineVideoFullscreenViewController"]) {
    return UIInterfaceOrientationMaskAll;
} else if  (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
    return UIInterfaceOrientationMaskLandscape;
} else {
    return UIInterfaceOrientationMaskPortrait;
} 

答案 1 :(得分:1)

这是一个解决方案,允许在iPhone应用程序中呈现的任何其他窗口(例如视频播放器)上旋转,但在iPad应用程序中保持风景。将其放在您的app delegate中。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {


if  (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
    return UIInterfaceOrientationMaskLandscape;
} else {

    if (window == self.window
        || ![window isMemberOfClass:[UIWindow class]]) {
        return UIInterfaceOrientationMaskPortrait;
    }

    if ([window isEqual:[[UIApplication sharedApplication] windows][1]]) {
        // Rotate the secondary window.
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }

    return UIInterfaceOrientationMaskPortrait;
}

答案 2 :(得分:0)

试一试......

-(BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

如果您不希望UIViewController在视频不在屏幕上时能够旋转。使用此 -

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(webView && webView.superView) return YES;
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}

答案 3 :(得分:0)

这与我为iOS 7解决的方式非常相似,但这对iOS 8不起作用。不再返回MPFullscreenWindow,Xcode抱怨打破约束。

答案 4 :(得分:0)

我的解决方案似乎相当普遍,并且在iOS 7上处理一些奇怪的行为,其中window有时会以nil传递。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        return UIInterfaceOrientationMaskAll;
    }
    else
    {
        // when dismissing a view controller, the view controller being returned to isn't in control of the orientiation
        // (shouldAutorotate and supportedInterfaceOrientations seem to be called, but they aren't observed then,
        // rather only when the device is rotated after that view is fully showing)
        // instead, the result of this method only is what's observed
        // we could return different values depending on which view controller is frontmost, but currently it seems
        // good enough to call supportedInterfaceOrientations of the frontmost view controller and return that

        // on ios7, critical calls to this method are often passed window=nil, in that case use self.window instead
        UIViewController *frontViewController = window ? window.rootViewController : self.window.rootViewController;

        // special case only when transitioning to or from a presented view controller
        if (frontViewController.presentedViewController &&
            (frontViewController.presentedViewController.isBeingDismissed || frontViewController.presentedViewController.isBeingPresented))
        {
            if (frontViewController.presentedViewController && !frontViewController.presentedViewController.isBeingDismissed) {
                frontViewController = frontViewController.presentedViewController;
            }
            if ([frontViewController isKindOfClass:[UINavigationController class]]) {
                frontViewController = ((UINavigationController *)frontViewController).topViewController;
            }

            // return whatever the front view controller's supportedInterfaceOrientations returns, since it normally is ignored for some reason
            return [frontViewController supportedInterfaceOrientations];
        }
        else
        {
            // return this normally, this gets intersected with the result of the front view controller's supportedInterfaceOrientations
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    }
}

答案 5 :(得分:0)

所以我想我会分享我的解决方案。实际上分离了OP问题,只是对其进行了修改。在iOS 7和8上适用于我。

我的方法不同,因为我在另一个启用纵向或横向的视图控制器中有一个切换开关。

无论如何,在这里。

更新:

好的,以前的方法打破了启动屏幕。示例:如果您的设备处于横向状态并已启动但它是一个纵向应用程序,它将旋转,但是,窗口将被切成两半。如果你有一些不错的加载屏幕,那么在对接特别是痛苦。无论如何,下面替换的代码修复了AND允许视频旋转。显然不是每个人都会使用旋转开关,只需相应调整。

//Allow video only rotation in portrait mode.
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{

    //Switch for Rotation
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    BOOL switchOn = [userDefaults boolForKey:@"Rotation"];

    if (switchOn) {
        window.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
        [window setFrame:[[UIScreen mainScreen] bounds]]; //Add
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    else {
        id presentedViewController = [window.rootViewController presentedViewController];
        NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;

        if ((window && [className isEqualToString:@"MPInlineVideoFullscreenViewController"]) ||
            [className isEqualToString:@"MPMoviePlayerViewController"] ||
            [className isEqualToString:@"AVFullScreenViewController"]) {

            window.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
            [window setFrame:[[UIScreen mainScreen] bounds]]; //Add
            [window makeKeyAndVisible];

            return UIInterfaceOrientationMaskAllButUpsideDown;
        }

        window.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
        [window setFrame:[[UIScreen mainScreen] bounds]]; //Add
        return UIInterfaceOrientationMaskPortrait;
    }

    self.window.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
    [self.window setFrame:[[UIScreen mainScreen] bounds]]; //Add
    [self.window makeKeyAndVisible];
}

再次更新:

所以我之前编辑的编辑在电影播放器​​下面有一个奇怪的视图闪烁。这似乎已经解决了这个问题。在6+设备上测试,在模拟器中测试iOS 7/8。

希望这有助于某人。

答案 6 :(得分:0)

=> Put below method inside your  AppDelegate class, it allow to play video in landscape mode even when device orientation is locked to portrait mode only:


- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)windowx
{
    if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]] ||
    [[self.window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(@"MPInlineVideoFullscreenViewController")])
    {
        if ([self.window.rootViewController presentedViewController].isBeingDismissed)
        {
            return UIInterfaceOrientationMaskPortrait;
        }
        else
        {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    }
    else
    {
        return UIInterfaceOrientationMaskPortrait;
    }
}

答案 7 :(得分:0)

Swift 3,我按照这种方式整理出来(你的info.plist /项目设置可以在ONYL上检查纵向方向):

// MARK: - Orientation

extension AppDelegate {
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

        // Let webView video to be rotated
        if window != self.window {
            return window?.rootViewController?.supportedInterfaceOrientations ?? .all;
        }

        // All other screens are portrait
        return .portrait;
    }

}