UIWebView背景设置为“清除颜色”,但不透明
我正在使用iOS SDK最新版本和XCode 4.2开发iOS 4应用程序。
我有一个与Alpha = 1.0 UIWebView的XIB, 背景设置为清除颜色和不透明没有设置。 在这个XIB上,我用这个代码设置了一个图像作为背景: 
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"AboutBackground.png"]]; self.view.backgroundColor = background; [background release]; } return self; } 
  UIWebView显示一个静态html: 
 <html><head></head><body style=\"margin:0 auto;text-align:center;background-color: transparent; color:white\">...</body></html> 
在iOS 5模拟器上,其背景是透明的,但在iOS 4设备上是灰色的。
任何线索?
还设置:
 [webView setBackgroundColor:[UIColor clearColor]]; [webView setOpaque:NO]; 
  /*for ios please set this*/ [webViewFirst setOpaque:NO]; /*for html please set this*/ <body style="background:none"> 
除了设置你的webview的背景来清除颜色,还要确保你设置不透明为false。
 webView.opaque = NO; webView.backgroundColor = [UIColor clearColor]; 
你可以试试这个代码(我知道,这是不安全的,但它甚至适用于ios5):
 - (void)makeBodyBackgroundTransparent { for (UIView *subview in [webView subviews]) { [subview setOpaque:NO]; [subview setBackgroundColor:[UIColor clearColor]]; } [webView setOpaque:NO]; [webView setBackgroundColor:[UIColor clearColor]]; } 
 NSString *content=@"example clear background color UIWebview"; NSString *style=[NSString stringwithformat:@"<html><head><style>body {background-color:transparent;}</style></head><body>%@</body></html>",content]; [myWebview loadhtmlstring:style baseurl:nil]; 
为了客观
 webView.opaque = NO; webView.backgroundColor = [UIColor clearColor]; 
请确保将其包含在您的HTML代码中:
 <body style="background-color: transparent;"> 
要么
  <body style="background:none">