How to turn YouTube embedded video into landscape mode

I use the following embed code to embed my YouTube videos in iOS

- (NSString*)embedYouTube:(NSString*)youtube_id frame:(CGRect)frame { NSString* embedHTML = @"\ <html><head>\ <style type=\"text/css\">\ body {\ background-color: transparent;\ color: white;\ }\ </style>\ </head><body style=\"margin:0\">\ <iframe src=\"http://www.youtube.com/embed/%@?rel=0\" frameborder=\"0\" allowfullscreen width=\"%0.0f\" height=\"%0.0f\"></iframe>\ </body></html>"; NSString *html = [NSString stringWithFormat:embedHTML, youtube_id, frame.size.width, frame.size.height]; return html; } //code to embed video NSString *contentHTML; if (currentAnswer.youtube_id != nil) { contentHTML = [self embedYouTube:currentAnswer.youtube_id frame:CGRectMake(CELL_TEXT_LEFT_MARGIN + CELL_AVATAR_WIDTH + CELL_SPACING, currentYAxisValue, CELL_YOUTUBEVIEW_WIDTH, CELL_YOUTUBEVIEW_HEIGHT)]; } [webView loadHTMLString: contentHTML baseURL:nil]; 

When I play a video, it only plays in potrait mode, not landscape mode. Is this a limitation due to iframes, is there any way to do this?

+6
source share
4 answers

It should work provided that your UIViewController can also rotate into terrain.

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

Verify that your UIViewController can rotate before trying to rotate the video. If you do not want your UIViewController to be able to rotate when the video is not displayed on the screen, you can simply do something like:

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

In your UIViewController, set its view to webView:

[self setView: webView];

Your webview does not receive rotation messages sent to the root view controller. You can also use the addChildViewController method if you made a separate view controller exclusively for your web browser.

0
source

This is almost the same as the other question I just answered, Fix the notification center orientation after resuming the application

YouTube videos have a subclass of UIViewController that represents them. They actually do not implement - (BOOL) shouldAutorotateToInterfaceOrientation; (what I know), and therefore use whatever orientation you are currently in.

If your application does not turn toward the landscape, it will also not be in the landscape. The setting [UIApplication sharedApplication] .statusbarOrientation should set the orientation of the Youtube video, however if you decide to do this only and then make a proposal to Michael Frederick (also), this may have some unusual effects when exiting the video (for example, a portrait of the user interface, but given the bands that overlap the user interface).

0
source
 #define RADIANS(degrees) ((degrees * M_PI) / 180.0) - (void) setTransformForCurrentOrientation { UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; NSInteger degrees = 0; if (UIInterfaceOrientationIsLandscape(orientation)) { if (orientation == UIInterfaceOrientationLandscapeLeft) { degrees = -90; } else { degrees = 90; } } else { if (orientation == UIInterfaceOrientationPortraitUpsideDown) { degrees = 180; } else { degrees = 0; } } rotationTransform = CGAffineTransformMakeRotation(RADIANS(degrees)); [UIView beginAnimations:nil context:nil]; [webView setTransform:rotationTransform]; [UIView commitAnimations]; } 
0
source

Source: https://habr.com/ru/post/919310/


All Articles