Can't change background for UIWebView in iOS

前端 未结 8 1182
小鲜肉
小鲜肉 2020-12-12 20:20

Is there a way to change background color for UIWebView?

None of the colors set in IB effect UIWebView behavior: before actual content is l

相关标签:
8条回答
  • 2020-12-12 20:50

    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)

    webview.backgroundColor = [UIColor clearColor];

    0 讨论(0)
  • 2020-12-12 20:51

    You can set the opaque property of the webview to NO and then set background color of the view beneath.

    [webView setOpaque:NO];
    
    0 讨论(0)
  • 2020-12-12 20:58

    I got a solution. Add the web view after it is loaded.

    -(void)webViewDidFinishLoad: (UIWebView *)pageView {
    
        self.view =pageView;
        [pageView release];
    }
    
    0 讨论(0)
  • 2020-12-12 21:09

    In 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;
    
    }
    
    0 讨论(0)
  • 2020-12-12 21:12

    Yes, it seems setting the backgroundColor property has no effect on the 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)

    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:

    - (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;
        }
    }
    
    0 讨论(0)
  • 2020-12-12 21:12

    Try this out. I have used a background image "address.png" from my application resources.

    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];
    
    0 讨论(0)
提交回复
热议问题