Gee*_*Out 2 uiwebview ios ios5
如果我创建它们呈现的内联样式,但是当尝试在xCode中的同一组和目录中引用css文件时,它们不会呈现.
这是我做的:
<html>
<head>
<link href="main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Hello, world! Learn</h1>
<p>
<a href="myscheme://advertising" class="buttonStyle">Click me!</a>
</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
然后我有一个名为main.css的css文件,如下所示:
body {background-color:#b0c4de;}
p {background-color:#e0ffff;}
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么这不会渲染CSS?我错过了HTML标题中的重要内容吗?
以下是我首先调用HTML的方法:
- (void)viewDidAppear:(BOOL)animated
{
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"learn"
ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[theWebView loadHTMLString:htmlString baseURL:nil];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:htmlFile]];
[theWebView loadRequest:request];
}
Run Code Online (Sandbox Code Playgroud)
小智 8
问题在于
[theWebView loadHTMLString:htmlString baseURL:nil];
Run Code Online (Sandbox Code Playgroud)
由于baseURL为nil,因此Web视图无法确定css文件的位置.不要乱用手动创建数据并加载它,而是将HTML文件的路径直接传递给UIWebView:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"learn" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *rq = [NSURLRequest requestWithURL:url];
[webView loadRequest:rq];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2190 次 |
| 最近记录: |