user3056487
user3056487

Reputation: 11

How to make uiwebview youtube video in landscape orientation when viewcontroller in portrait

I'm using embedded youtube in my UIWebView , my viewcontroller is in portrait mode but I am unable to see the video in fullscreen landscape . I tried many solutions from Stackoverflow , but none are working on iOS 7.

Upvotes: 1

Views: 717

Answers (2)

ThE uSeFuL
ThE uSeFuL

Reputation: 1534

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receivedRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)dealloc{

    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(void) receivedRotate: (NSNotification*) notification
{


        UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
        //Using this part to find the view controller on top (the one that's showing the video in fullscreen).

        while (topController.presentedViewController) {
            topController = topController.presentedViewController;
        }



        if ([topController isKindOfClass:NSClassFromString(@"AVFullScreenViewController")]) {

            switch ([UIDevice currentDevice].orientation) {
                case UIDeviceOrientationFaceUp:
                    NSLog(@"UIDeviceOrientationFaceUp");
                    break;
                case UIDeviceOrientationFaceDown:
                    NSLog(@"UIDeviceOrientationFaceDown");
                    break;
                case UIDeviceOrientationLandscapeLeft:
                    NSLog(@"UIDeviceOrientationLandscapeLeft");
                    topController.view.transform = CGAffineTransformMakeRotation(M_PI_2);
                    break;
                case UIDeviceOrientationLandscapeRight:
                    NSLog(@"UIDeviceOrientationLandscapeRight");
                    topController.view.transform = CGAffineTransformMakeRotation(-M_PI_2);
                    break;
                default:
                    break;
            }


            topController.view.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height);
        }

}

Upvotes: 0

Lord Zsolt
Lord Zsolt

Reputation: 6557

When I had a similar problem to yours, what I did was:

  1. Even though it's portrait only, you can get the device orientation.
  2. If the orientation is landscape, you can transform the status bar by rotating, and same thing with the MPMoviePlayerController.

Here's my code I used (it's for iOS6 though, so it might be different

-(void) receivedRotate: (NSNotification*) notification
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    //Using this part to find the view controller on top (the one that's showing the video in fullscreen).

    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }

    //After a little testing, the class of that controller is MPInlineVideoFullscreenViewController
    if ([topController isKindOfClass:NSClassFromString(@"MPInlineVideoFullscreenViewController")]) {
        topController.view.transform = CGAffineTransformMakeRotation(M_PI_2);

        //The 20 and -20 are to prevent the movie from going over the status bar
        topController.view.frame = CGRectMake(0, 20, self.view.frame.size.width,self.tabBarController.view.frame.size.height - 20);
    }
}

Upvotes: 1

Related Questions