为UIWebView设置背景图像

时间:2014-07-05 06:35:48

标签: ios uiwebview background-image

您好我有一个UIWebView,我已经为我的UIWebView设置了背景图像,但问题是图像在我的UIWebView中显示更多时间请告诉我们如何解决这个问题。

我的代码。

   - (void)viewDidLoad
    {
       [super viewDidLoad];
       [self.webview setOpaque:NO];
       [self.webview setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]]];
       [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"about" ofType:@"html"] isDirectory:NO]]];

    }

我已经使用了上面的内容,但它没有正确显示它的下半部分或显示更多时间请告诉我如何解决此问题

1 个答案:

答案 0 :(得分:0)

您可以将UIWebView的背景设置为clearColor,并在其背后设置UIImage

UIImageView *background = [[UIImageView alloc] initWithFrame:CGRectMake(webviewOriginX, webviewOriginY, webview.size.width, webviewSizeHeight)];
background.image = [UIImage imageNamed:@"bg.png"]];
[self.view sendSubviewToBack:background];

[self.view addSubview:background];

// init your webview here
[self.webview setBackgroundColor:[UIColor clearColor];

编辑:样本

尝试在ViewController中注入这些小代码:

    - (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:path];

    UIImageView *background = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    background.backgroundColor = [UIColor orangeColor];

    [self.view addSubview:background];

    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];

    NSString *htmlCode = [NSString stringWithFormat:@"<html><title>TITLE</title><body>HELLO WORLD</body>"];

    [webView loadHTMLString:htmlCode baseURL:baseURL];
    webView.backgroundColor = [UIColor clearColor];
    [webView setOpaque:NO];

    [self.view addSubview:webView];
}