Sukhpreet
Sukhpreet

Reputation: 943

Fit YouTube video thumbnail in UIWebView on video start up

I'm loading Youtube video inside UIWebView using following code.

let videoUrl = "https://www.youtube.com/embed/F9GujgK0y2M"
let embedHTML = "<iframe width=\(self.webView.frame.size.width)\" height=\(self.webView.frame.size.height)\" src=\(videoUrl)?&playsinline=1\" frameborder=\"0\" allowfullscreen></iframe>"

Starting thumbnail for video is very large. I need to fit it according to size of UIWebView. Please help me, How can I change it ?enter image description here

Upvotes: 3

Views: 1288

Answers (3)

MLQ
MLQ

Reputation: 13511

For me, removing the height and width properties in the iframe HTML tag made the frame fit the page width altogether.

Upvotes: 0

nanibir
nanibir

Reputation: 234

Try this:

webView.scalesPageToFit = true

From Apple Documentation, setting this to true scales the webpage to fit the screen.

Upvotes: 1

mugx
mugx

Reputation: 10105

maybe this code can help you:

let yourVideoID = "F9GujgK0y2M"
let css = ".video-container {position:relative;padding-bottom:56.25%;height:0;overflow:hidden;} .video-container iframe, .video-container object, .video-container embed { position:absolute; top:0; left:0; width:100%; height:100%; }"
let url = "https://www.youtube.com/embed/\(yourVideoID)?playsinline=1&modestbranding=1&showinfo=0&rel=0&showsearch=0&loop=1&iv_load_policy=3"
let htmlString = "<html><head><style type=\"text/css\">\(css)</style></head><body><div class=\"video-container\"><iframe src=\"\(url)\" frameborder=\"0\"></iframe></div></body></html>"

yourWebView.scalesPageToFit = false
yourWebView.allowsInlineMediaPlayback = true;
yourWebView.loadHTMLString(htmlString, baseURL: nil)

Upvotes: 1

Related Questions