UIWebView breaking constraint when playing a video

2019-07-22 15:45发布

问题:

I'm updating an iOS app for an YouTube channel. In the previous version (iOS 7 using Xcode 5), I used to embed youtube iFrame in a UIWebView and everything would work just fine.

However, in iOS 8 using Xcode 6, every time I play a Youtube video and it goes full screen, my UIWebView constraints break and UIWebView gets relocated in my Controller (usually goes 10 points up in the screen).

I've tried to change the constraints but it seems that doesn't matter what constraint I set up, it will eventually break when the player goes full screen.

Here's the html code used to embed YouTube iframe:

- (void)embedYouTube:(NSString *)urlString
{
NSString *embedHTML =[NSString stringWithFormat:@"\
                      <html><head>\
                      <style type=\"text/css\">\
                      body {\
                      background-color: transparent;\
                      color: blue;\
                      }\
                      </style>\
                      </head><body style=\"margin:0\">\
                      <iframe height=\"140\" width=\"325\"      src=\"http://www.youtube.com/embed/%@\"></iframe>\
                      </body></html>",urlString];
[self.webView loadHTMLString:embedHTML baseURL:nil];

}

Since the log is huge, I've pasted it in pastebin: http://pastebin.com/z4zyq7Hs

Thank you :)

回答1:

I had same problem. But, I do not experience relocation problem though (using Xcode 6.1), only bunch of constraint breaking logs.

What I tested that still cannot work: - remove constraint and setTranslatesAutoresizingMaskIntoConstraints to YES for all the webview and its subview - swap UIWebview with WKWebview, the newer and faster UIWebview

I also test playing Dailymotion video and it also has same problem, so I suspect this is more the problem on MediaPlayer views of UIWebview.



回答2:

try like this

- (void)embedYouTube:(NSString*)url frame:(CGRect)frame {  
     NSString* embedHTML = @"\ 
        <html><head>\ 
     <style type=\"text/css\">\ 
     body {\ 
     background-color: transparent;\ 
     color: white;\ 
     }\ 
     </style>\ 
     </head><body style=\"margin:0\">\ 
        <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \ 
     width=\"%0.0f\" height=\"%0.0f\"></embed>\ 
        </body></html>";  
     NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];  
     if(videoView == nil) {  
       videoView = [[UIWebView alloc] initWithFrame:frame];  
       [self.view addSubview:videoView];  
     }  
     [videoView loadHTMLString:html baseURL:nil];  
    }  


回答3:

I'm about to leave work so I'll have to type this answer quickly,

one way I have found of fixing this is refreshing the cache of the page being viewed, I don't have a solid fix for this but I came up with a temporary one last week, this can be done by creating a link to a css file in the header of your html

<link id="refreshCache" rel="stylesheet" type="text/css" href="refresh.css">

it's important that you also create a refresh.css file, you are however aloud to leave it blank,

now using javascript or jQuery if you already have it linked up, use the

.remove http://api.jquery.com/remove/

on the id refreshCache, on page load, removing the link in the header to the empty file when the page loads...

this will refresh your browsers cache, hope this helps!