简体   繁体   English

在UIWebView上显示背景图像

[英]Displaying a background image on UIWebView

I'm trying to build a native application that for all intents and purposes is HTML/CSS and Javascript. 我正在尝试构建一个本机应用程序,所有意图和目的是HTML / CSS和Javascript。 It all works fine, but when the UIWebView loads there's a delay while the DOM is populated, images retrieved etc. 一切正常,但是当UIWebView加载时,在填充DOM,检索图像等时会出现延迟。

Is there anyway to display a background image on the UIWebView, or something behind it? 无论如何在UIWebView上显示背景图像,或者背后的东西? Ideally this will be the same as the splash screen so it doesn't affect the experience of transitioning from slash to app. 理想情况下,它与启动画面相同,因此不会影响从斜杠转换为应用程序的体验。

Thanks 谢谢

Robbie 罗比

I've had to do the following in my code to get a transparent background on my UIWebViews - I usually do this in the viewDidLoad method of the UIViewController 我必须在我的代码中执行以下操作才能在UIWebViews上获得透明背景 - 我通常在UIViewController的viewDidLoad方法中执行此操作

self.webView.backgroundColor = [UIColor clearColor];
self.webView.opaque = NO;

I think you can also make those changes in Interface Builder in the UIWebViews properties. 我想你也可以在UIWebViews属性中的Interface Builder中进行这些更改。

--- edit --- ---编辑---

And for the background image you can just put a UIImageView in behind the UIWebView now that you've given it a transparent background. 对于背景图像,您可以将UIImageView放在UIWebView后面,因为您已经为它提供了透明背景。

 webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"senthil.jpg"]];

I don't believe you want to be setting the UIWebView as transparent and showing an image behind it. 我不相信您希望将UIWebView设置为透明并在其后面显示图像。

Instead you should have some sort of view (maybe with a picture as you suggest, maybe with a UIIndicator and a semi-transparent view that "darkens" the page) that sits there until the UIWebView has loaded its content. 相反,你应该有一些视图(可能有一个你建议的图片,可能有一个UIIndicator和半透明的视图,使页面“变暗”),直到UIWebView加载了它的内容。

To do this, first your class should subscribe to the UIWebViewDelegate protocol: 要做到这一点,首先你的类应该订阅UIWebViewDelegate协议:

@interface myWebView : UIViewController <UIWebViewDelegate> {
  // variables etc
}

Then, you can show a view on load: 然后,您可以显示加载视图:

-(void)viewDidLoad {
  myView.hidden = NO;
}

And finally, you can override the webViewDidFinishLoad method to hide it once it's loaded 最后,您可以覆盖webViewDidFinishLoad方法,以便在加载后隐藏它

- (void)webViewDidFinishLoad:(UIWebView *)localwebView {
    myView.hidden = YES;
}

Add the Image to View Controller in XIB or to Plist of "Launch Image". 将图像添加到XIB中的视图控制器或“启动图像”的Plist。 This will show the Splash Screen from Beginning. 这将显示从Beginning开始的启动画面。 While loading the webView in viewDidLoad hide the webView 在viewDidLoad中加载webView时隐藏webView

webView.hidden = YES;

Load the webView with HTML content as usual in viewDidLoad 像往常一样在viewDidLoad中加载带有HTML内容的webView

Once the webView is loaded, perform updating the view after some. 加载webView后,执行一些更新后的视图。 I think this will allow some time for delegate function to complete and proceed with UI updates. 我认为这将允许一些时间来完成委托功能并继续进行UI更新。

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self performSelector:@selector(updateWebView) withObject:nil afterDelay:1];
}

-(void) updateWebView {
    webView.hidden = NO;
    self.view = webViewElement;
    // or
    [self.view addSubview:webViewElement];
}

I tried as above is working fine with no white Screen. 我尝试如上所述工作正常,没有白色屏幕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM