无法在 iOS 中更改 UIWebView 的背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1547102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't change background for UIWebView in iOS
提问by leon
Is there a way to change background color for UIWebView
?
有没有办法改变背景颜色UIWebView
?
None of the colors set in IB effect UIWebView
behavior: before actual content is loaded it shows as up as white (causing a white flash between the moment it is loaded and content is rendered).
IB 效果UIWebView
行为中没有设置任何颜色:在加载实际内容之前,它显示为白色(在加载和渲染内容之间导致白色闪烁)。
Setting background color programmatically does not do anything either.
以编程方式设置背景颜色也没有任何作用。
Here is code:
这是代码:
@interface Web_ViewViewController : UIViewController {
UIWebView *web;
}
@property (nonatomic, retain) IBOutlet UIWebView *web;
@end
....
-(void)viewDidLoad {
super viewDidLoad;
web.backgroundColor = [UIColor blueColor];
NSURL *clUrl = [NSURL URLWithString:@"http://www.apple.com"];
NSURLRequest *req = [NSURLRequest requestWithURL:clUrl];
[web loadRequest:req];
}
回答by Kaveh
You can set the opaque property of the webview to NO and then set background color of the view beneath.
您可以将 webview 的 opaque 属性设置为 NO,然后设置下方视图的背景颜色。
[webView setOpaque:NO];
回答by Adolfo
Give this a try. It will fade the UIWebView in once it has finished loading and you won't see the flash.
试试这个。加载完成后,它会将 UIWebView 淡入淡出,您将看不到 Flash。
@interface Web_ViewViewController : UIViewController <UIWebViewDelegate> {
UIWebView *web;
BOOL firstLoad;
}
@property (nonatomic, retain) IBOutlet UIWebView *web;
@end
...
(void)viewDidLoad {
[super viewDidLoad];
firstLoad = YES;
web.delegate = self;
web.alpha = 0.0;
NSURL *clUrl = [NSURL URLWithString:@"http://www.apple.com"];
NSURLRequest *req = [NSURLRequest requestWithURL:clUrl];
[web loadRequest:req];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (firstLoad) {
firstLoad = NO;
[UIView beginAnimations:@"web" context:nil];
web.alpha = 1.0;
[UIView commitAnimations];
}
}
The animation block in webViewDidFinishLoad will fade the view in once it's loaded, or if you prefer to have it pop on then just remove the UIView calls.
webViewDidFinishLoad 中的动画块将在视图加载后淡入淡出,或者如果您希望它弹出,则只需删除 UIView 调用。
回答by Bogatyr
None of the answers here worked for me, they all involved still some sort of flash. I found a working answer here: http://www.iphonedevsdk.com/forum/iphone-sdk-development/4918-uiwebview-render-speeds-white-background.html(I had to adjust the delay to .25 to avoid the flashing). Remember to check the UIWebView's "hidden" flag in InterfaceBuilder.
这里的答案都不适合我,它们都涉及某种闪光。我在这里找到了一个有效的答案:http: //www.iphonedevsdk.com/forum/iphone-sdk-development/4918-uiwebview-render-speeds-white-background.html(我不得不将延迟调整为 0.25 以避免闪烁)。记得在 InterfaceBuilder 中检查 UIWebView 的“隐藏”标志。
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[self performSelector:@selector(showAboutWebView) withObject:nil afterDelay:.25];
}
- (void)showAboutWebView
{
self.infoWebView.hidden = NO;
}
回答by user377808
I got a solution. Add the web view after it is loaded.
我得到了解决方案。加载后添加 web 视图。
-(void)webViewDidFinishLoad: (UIWebView *)pageView {
self.view =pageView;
[pageView release];
}
回答by pythonquick
Yes, it seems setting the backgroundColor property has no effect on the UIWebView.
是的,似乎设置 backgroundColor 属性对 UIWebView 没有影响。
I don't have a satisfactory solution, only a workaround that you can consider.
我没有令人满意的解决方案,只有您可以考虑的解决方法。
- First change the background color, by setting an initial empty HTML page
- Once that has loaded, you load your main URL (e.g. http://www.apple.com)
- 首先改变背景颜色,通过设置一个初始的空 HTML 页面
- 加载后,您将加载您的主 URL(例如http://www.apple.com)
If you don't wait for the initial HTML page to load, you might not see the initial background while the main URL is loading. So you'll need to use the UIWebViewDelegate's webViewDidFinishLoad: method. You can implement that method in your Web_ViewViewController class, and make your Web_ViewViewController conform to the UIWebViewDelegate protocol. Initially, there is still a momentary flicker of white background until the empth HTML page loads. Not sure how to get rid of that. Below is a sample:
如果您不等待初始 HTML 页面加载,则在加载主 URL 时您可能看不到初始背景。所以你需要使用 UIWebViewDelegate 的 webViewDidFinishLoad: 方法。您可以在您的 Web_ViewViewController 类中实现该方法,并使您的 Web_ViewViewController 符合 UIWebViewDelegate 协议。最初,在加载空白 HTML 页面之前,白色背景仍然会短暂闪烁。不知道如何摆脱它。下面是一个示例:
- (void)viewDidLoad
{
[web loadHTMLString: @"<html><body bgcolor=\"#0000FF\"></body></html>" baseURL: nil];
web.delegate = self;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
static BOOL loadedMainURLAlready = NO;
if (!loadedMainURLAlready)
{
NSURL *clUrl = [NSURL URLWithString:@"http://apple.com"];
NSURLRequest *req = [NSURLRequest requestWithURL:clUrl];
[webView loadRequest:req];
loadedMainURLAlready = YES;
}
}
回答by Sagar R. Kothari
Try this out. I have used a background image "address.png" from my application resources.
试试这个。我使用了我的应用程序资源中的背景图像“address.png”。
NSString *path=[[NSBundle mainBundle] pathForResource:@"address" ofType:@"png"];
NSURL *a=[[NSURL alloc] initFileURLWithPath:[path stringByDeletingLastPathComponent] isDirectory:YES];
NSLog(@"%@",[a description]);
NSDictionary *d=[parsedarray objectAtIndex:0];
// generate dynamic html as you want.
NSMutableString *str=[[NSMutableString alloc] init];
[str appendString:@"<html><body background=\"address.png\"><table><tr>"];
[str appendFormat:@"<td valign=\"top\" align=\"left\"><font size=\"2\"><b>Market Address</b><br>%@,<br>%@,<br>%@</font></td>",
([d valueForKey:@"address1"])?[d valueForKey:@"address1"]:@"",
([d valueForKey:@"address2"])?[d valueForKey:@"address2"]:@"",
([d valueForKey:@"address3"])?[d valueForKey:@"address3"]:@""
];
[str appendFormat:@"<td valign=\"top\" align=\"left\"><font size=\"2\"><b>Contact Number</b><br>%@<br><b><a href=\"%@\">Email Us</a><br><a href=\"%@\">Visit Website</a></b></font></td>",
[d valueForKey:@"phone"],[d valueForKey:@"email"],[d valueForKey:@"website"]];
[str appendString:@"</tr></table></body></html>"];
// set base url to your bundle path, so that it can get image named address.png
[wvAddress loadHTMLString:str baseURL:a];
[str release]; str=nil;
[a release];
回答by Drew Harris
In Xcode 4.6.3
在 Xcode 4.6.3 中
- (void)viewDidLoad
{
[super viewDidLoad];
self.viewWeb.hidden = YES;
self.viewWeb.delegate = self;
// this allows the UIWebView box to your background color seamlessly.
self.viewWeb.opaque = NO;
}
回答by Matthew Ferguson
This might help
这可能有帮助
(Xcode 5 iOS 7) Universal App example for iOS 7 and Xcode 5. It is an open source project / example located here: Link to SimpleWebView (Project Zip and Source Code Example)
(Xcode 5 iOS 7) 适用于 iOS 7 和 Xcode 5 的通用应用程序示例。它是一个开源项目/示例,位于此处:链接到 SimpleWebView(项目邮编和源代码示例)
webview.backgroundColor = [UIColor clearColor];
webview.backgroundColor = [UIColor clearColor];