天天看点

ios webview设置背景色

需注意2点

1.webview添加到视图上之后,再运行注入改变背景色的js代码有效,否则无效

2.如果webview内容从服务端load,最好再次webview代理方法中viewdidfinashed里注入改变颜色的js代码,因为服务端传来的html代码可能也包含改变背景色的,可能与你写的冲突,所以在加载完服务端之后,需要在finish方法里再注入一次。

总结截图如下:

self.bottomTip = [[UIWebView alloc] initWithFrame:CGRectMake(13, ZYPscreenBounds.size.height-20-100, ZYPscreenBounds.size.width-13*2, 100)];
    self.bottomTip.scrollView.scrollEnabled = NO;
    [self.view addSubview:self.bottomTip];
    [self.bottomTip stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#F0F0F0'"];
    self.bottomTip.delegate = self;
           
-(void)webViewDidFinishLoad:(UIWebView *)webView{
//背景色
self.bottomTip.backgroundColor = [UIColor clearColor];
    [self.bottomTip stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#F0F0F0'"];
}
           

更多问题,加群交流:565191947