代码之家  ›  专栏  ›  技术社区  ›  leon

无法在iOS中更改uiwebview的背景

  •  58
  • leon  · 技术社区  · 15 年前

    有没有办法改变背景色 UIWebView ?

    没有IB效果中设置的颜色 UIWebVIEW 行为:在加载实际内容之前,它显示为白色(在加载内容和呈现内容之间产生白色闪光)。

    以编程方式设置背景色也不起任何作用。

    代码如下:

    @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];
    }
    
    8 回复  |  直到 6 年前
        1
  •  180
  •   Cœur N0mi    6 年前

    可以将webview的opaque属性设置为no,然后在下面设置视图的背景色。

    [webView setOpaque:NO];
    
        2
  •  17
  •   Adolfo    15 年前

    试试这个。加载完成后,它将淡入uiwebview,您将看不到闪光灯。

    @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];
        }
    }
    

    webviewdidfinishload中的动画块在加载后将淡入,或者如果希望打开它,则只需删除uiview调用。

        3
  •  7
  •   Bogatyr    14 年前

    这里没有一个答案对我有用,它们都涉及到某种闪光。我在这里找到了一个有效的答案: http://www.iphonedevsdk.com/forum/iphone-sdk-development/4918-uiwebview-render-speeds-white-background.html (我不得不将延迟时间调整到0.25以避免闪烁)。记住在interfacebuilder中检查uiwebview的“hidden”标志。

    - (void)webViewDidFinishLoad:(UIWebView *)webView 
    {
        [self performSelector:@selector(showAboutWebView) withObject:nil afterDelay:.25];          
    }
    
    - (void)showAboutWebView 
    {
        self.infoWebView.hidden = NO;
    }
    
        4
  •  1
  •   pythonquick    15 年前

    是的,设置backgroundcolor属性似乎对uiwebview没有影响。

    我没有一个满意的解决方案,只是一个你可以考虑的解决办法。

    • 首先,通过设置一个初始的空html页面来更改背景色
    • 加载后,您将加载主url(例如 http://www.apple.com )

    如果不等待初始html页面加载,则在加载主url时可能看不到初始背景。所以您需要使用uiWebViewDelegate的webViewDidFinishload:方法。您可以在Web视图控制器类中实现该方法,并使Web视图控制器符合uiWebViewDelegate协议。最初,在empth 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;
        }
    }
    
        5
  •  1
  •   user377808    14 年前

    我有办法了。 加载后添加Web视图。

    -(void)webViewDidFinishLoad: (UIWebView *)pageView {
    
        self.view =pageView;
        [pageView release];
    }
    
        6
  •  0
  •   sagarkothari    14 年前

    试试这个。我使用了应用程序资源中的背景图像“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];
    
        7
  •  0
  •   Drew Harris    11 年前

    在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;
    
    }
    
        8
  •  0
  •   Matthew Ferguson    9 年前

    这可能会有帮助

    (Xcode 5 iOS 7)iOS 7和Xcode 5的通用应用程序示例。它是一个开源项目/示例,位于以下位置: Link to SimpleWebView (Project Zip and Source Code Example)

    webview.backgroundcolor=[uicolor clearcolor];