我使用WKWebView加载从服务器返回的html字符串,并设置CSS,但页面没有显示手机屏幕,我该怎么办?
NSString *requestUrl = [NSString stringWithFormat:@"http://news-at.zhihu.com/api/4/news/%ld",self.story.ID];
requestSuccessBlock sblock = ^(id responseObject){
YAContentItem *content = [YAContentItem contentItemWithKeyValues:responseObject];
NSString *htmlString = [NSString stringWithFormat:@"<html><head><link rel=\"stylesheet\" type=\"text/css\" href=%@ /></head><body>%@</body></html>", content.css.firstObject, content.body];
[self.webView loadHTMLString:htmlString baseURL:nil];
答案 0 :(得分:0)
- (WKWebView *)webView {
if (_webView == nil) {
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 44)];
// 自适应屏幕宽度js
NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *contentController = webView.configuration.userContentController;
// 添加自适应屏幕宽度js调用的方法
[contentController addUserScript:wkUserScript];
webView.navigationDelegate = self;
[self.view addSubview:webView];
_webView = webView;
[self.view addSubview:webView];
}
return _webView;
}