【问题标题】:Why Is Youtube Video Not Filling UiWebView?为什么 Youtube 视频没有填满 UiWebView?
【发布时间】:2013-03-07 05:39:01
【问题描述】:

我是 iphone 开发的菜鸟,我无法在 UIwebview 中显示嵌入的 youtube 视频。目前,youtube 视频位于 uiwebview 的左上角,而且它还有一个烦人的边框。我将 youtube 框架设置为与我的 uiwebview 相同的尺寸,并且 webview 设置为“缩放以适应”。如何使我的 youtube 内容适合整个屏幕并摆脱边框?非常感谢任何帮助。

我的代码

NSString *video_ID = youtubeid;
NSString *htmlStr = [NSString stringWithFormat:@"<iframe class=\"youtube-player\" type=\"text/html\" width=\"320\" height=\"230\" src=\"http://www.youtube.com/embed/%@\" frameborder=\"0\"></iframe>",video_ID];
[videoScreen loadHTMLString:htmlStr baseURL:nil]; 

编辑

 NSString *video_ID = youtubeid;
NSString *htmlStr = [NSString stringWithFormat:@"<iframe class=\"youtube-player\" type=\"text/html\" width=\"100%\" height=\"100%\" src=\"http://www.youtube.com/embed/%@\" frameborder=\"0\"></iframe>",video_ID];
[videoScreen loadHTMLString:htmlStr baseURL:nil]; 

【问题讨论】:

  • 您明确将宽度和高度设置为 320x230。试试100%的?或使用 javascript 以编程方式创建小部件并获取 Web 视图的尺寸。
  • 感谢您的回复。如何/在哪里将尺寸设置为 100%
  • 内联 iframe 定义,您在其中指定 width=\"320\" height=\"230\"
  • 它没有用。请检查我的编辑。
  • 确认;我需要玩它——对不起。后一种建议可能更好(使用 JS 以编程方式创建),但显然不是那么容易。

标签: iphone html ios uiwebview youtube


【解决方案1】:

你试过了吗……

NSString *video_ID = youtubeid;
NSString *htmlStr = [NSString stringWithFormat:@"<iframe class=\"youtube-player\" type=\"text/html\" width=\"100%%\" height=\"100%%\" src=\"http://www.youtube.com/embed/%@\" frameborder=\"0\"></iframe>",video_ID];
[videoScreen loadHTMLString:htmlStr baseURL:nil];

注意 %% 用于 % 字符。

【讨论】:

  • 这适用于全宽显示,但仅显示半高 (115)。有什么想法吗?
  • scale to fit in uiwebview 用于根据 web 视图的大小缩放 web 视图的包含.....只需将 web 视图的高度从 115 更改为 320 并查看.. ...
【解决方案2】:

所以首先你必须创建一个 UIWebView 并将它添加到你的主视图中。 在下面,我创建了一个指向 www.example.com 的 webview,我在其中嵌入了我想要播放的 YouTube 剪辑,我将其设置为隐藏,因为我稍后想要全屏启动视频。`

UIWebView *htmlView;
 htmlView = [[UIWebView alloc] initWithFrame:CGRectMake( 55.0f, 105.0f, 369.0f, 178.0f) ];  
  [htmlView setBackgroundColor:[UIColor blackColor]];  
    htmlView.hidden = YES;  
     htmlView.delegate = self;  
      [self addSubview:htmlView];  
       [htmlView loadRequest:[NSURLRequest requestWithURL:[NSURL   URLWithString:@"www.example,com"]]];  `

【讨论】:

    【解决方案3】:

    所以在这里我扫描了 web 视图中的所有按钮,然后选择使用 sendActionsForControlEvents 函数找到的第一个按钮。

    UIButton* findButtonInView(UIView* view)  
     {  
     UIButton *button = nil;  
      if( [view isMemberOfClass:[UIButton class]] )  
        {  
      return (UIButton*)view;  
       }  
    
        if( view.subviews && [view.subviews count] > 0 )  
        {  
        for( UIView *subview in view.subviews )  
        {  
        button = findButtonInView( subview );  
        if( button )   
        {  
          return button;  
      }  
       }  
        }  
    
         return button;  
          }  
    
        -(void)webViewDidFinishLoad:(UIWebView*)webView  
         {  
        UIButton *button = findButtonInView( webView );  
          if( button != nil )  
           {  
          [button sendActionsForControlEvents:UIControlEventTouchUpInside];  
      }  
       }  
    

    【讨论】:

      猜你喜欢
      • 2011-04-20
      • 2012-12-09
      • 2013-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-17
      • 2015-05-05
      • 2021-12-19
      相关资源
      最近更新 更多