iOS: How to make UIWebView 50% transparent?
I want to make a UIWebView whose background is 50% transparent, but w开发者_如何学Pythonhose content e.g. are not transparent at all.
Is such a thing possible?
Thanks.
webView.alpha=0.5f;
[webView setBackgroundColor:[UIColor clearColor]];
[webView setOpaque:NO];
It looks like this is possible, maybe. See this blog post:
http://erikloehfelm.blogspot.com/2009/04/transparent-background-on-iphone.html
Making the background semi-transparent depends on whether or not you can set the CSS color of the web page's BODY element to a semi-transparent color (instead of "transparent").
Also, this blog post doesn't show how to do this if you don't control the HTML of the web page you're trying to display, but it's possible with the UIWebView
to programmatically change this color once the page contents have loaded.
Play around with changing transparent to whatever color / alpha for the webView.backgroundColor
and the html <body>
css style to find a combination that you like best. Example with loadHTMLString
:
- (void)embedYouTubeWithVideoID:(NSString *)videoID {
webView.backgroundColor = [UIColor clearColor];
CGFloat w = webView.frame.size.width;
CGFloat h = webView.frame.size.height;
NSString *ytUrlString = [NSString stringWithFormat:@"http://www.youtube.com/v/%@&version=3&autohide=1&autoplay=1&cc_load_policy=1&fs=1&hd=1&modestbranding=1&rel=0&showsearch=0", videoID];
NSString *embed = [NSString stringWithFormat:@"\
<html>\
<head>\
<meta name=\"viewport\" content=\"initial-scale = 1.0, user-scalable = no, width = %0.0f\"/>\
</head>\
<body style=\"background:transparent; margin-top:0px; margin-left:0px\">\
<div>\
<object width=\"%0.0f\" height=\"%0.0f\">\
<param name=\"movie\" value=\"%@\" />\
<param name=\"wmode\" value=\"transparent\" />\
<param name=\"allowFullScreen\" value=\"true\" />\
<param name=\"quality\" value=\"high\" />\
<embed src=\"%@\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowscriptaccess=\"always\" wmode=\"transparent\" width=\"%0.0f\" height=\"%0.0f\" />\
</object>\
</div>\
</body>\
</html>", w, w, h, ytUrlString, ytUrlString, w, h];
[webView loadHTMLString:embed baseURL:nil];
}
精彩评论