如何将嵌入的YouTube视频旋转到横向模式

Zhe*_*hen 6 embed youtube landscape uiinterfaceorientation ios

我正在使用以下嵌入代码在IOS上嵌入我的YouTube视频

- (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];
Run Code Online (Sandbox Code Playgroud)

当我播放视频时,它只播放在potrait模式而不是横向模式.这是由于'iframes'的限制吗,有什么方法可以解决这个问题吗?

Mic*_*ick 2

如果您的 UIViewController 也能够旋转到横向,它应该可以工作。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

在尝试旋转视频之前测试您的 UIViewController 是否能够旋转。如果您不希望 UIViewController 在视频不在屏幕上时能够旋转,您可以执行以下操作:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if(webView && webView.superView) return YES;
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
Run Code Online (Sandbox Code Playgroud)