Ios 为UIWebView设置背景图像

Ios 为UIWebView设置背景图像,ios,uiwebview,background-image,Ios,Uiwebview,Background Image,您好,我有一个UIWebView,我已经为我的UIWebView设置了背景图像,但问题是图像在我的UIWebView中显示的时间更长。请告诉我如何解决此问题 我的密码 - (void)viewDidLoad { [super viewDidLoad]; [self.webview setOpaque:NO]; [self.webview setBackgroundColor:[UIColor colorWithPatternImage:[U

您好,我有一个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]]];

    }

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

您可以将
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];
}
-(void)viewDidLoad
{
[超级视图下载];
NSString*path=[[NSBundle mainBundle]bundlePath];
NSURL*baseURL=[NSURL fileURLWithPath:path];
UIImageView*background=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,200,200)];
backgroundColor=[UIColor orangeColor];
[self.view addSubview:background];
UIWebView*webView=[[UIWebView alloc]initWithFrame:CGRectMake(0,02000)];
NSString*htmlCode=[NSString stringWithFormat:@“TITLEHELLO WORLD”];
[webView loadHTMLString:htmlCode baseURL:baseURL];
webView.backgroundColor=[UIColor clearColor];
[网络视图设置不透明:否];
[self.view addSubview:webView];
}

在webview加载数据时是否需要该图像?@YogeshSuthar是的,我的应用程序中本地有一个html文件,因此我想显示backgound图像,但它在我的webview@user3793919我添加了一个示例代码,使用它,您将理解我试图实现的逻辑,您只需修复维度并更改
webview.backgroundColor