用CSS做iOS和Android样式适配

html-css013

用CSS做iOS和Android样式适配,第1张

在移动端webview渲染的时候,html标签会被自动加入一个class属性,会标识不同的设备

如:

所以,可以用来适配一些移动双端的差异场景,如适配不同高度Titlebar

不同样式:

使用样式:

在iOS设备上就会展示80px,Android设备上60px

系统故障。系统故障导致ios16css滚动背景颜色和边框颜色消失,是需要等待系统恢复的,CSS是CascadingStyleSheets的简称,中文称为层叠样式表。属性和属性值用冒号隔开,以分号结尾。

思路:对html进行操作,然后webView loadHtml

将需要的js、css资源导入创建好的bundle下,如上图

WKWebView *webView = [[WKWebView alloc]initWithFrame:frame]]

这个filePaths 就是需要加载的 js、css文件在本地的路径,是个数组,因为可能需要加载本地的多个js、css文件, 如果需要加载的js、css文件较多,可以让后台传给你对应js、css文件的路径,注意传的路径要跟导入本地的资源路径一致,否则会加载失败。

//路径path

NSArray *pathArray  = dict[@"filePath"]

if (pathArray) {            

NSError *error            

//获取网络的HTML            

NSString * online_HTML  = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:&error]           

 if(!error) {                

//以分割               

 NSArray * array_HTML    = [online_HTML componentsSeparatedByString:@"</head>"]                                

NSMutableString *header_HTML = [[NSMutableString alloc]initWithString:array_HTML.firstObject]                                

for (NSString *path in pathArray) {                    

//注意这里的hightcharts.bundle,更改成你本地的bundle名

NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"hightcharts.bundle/%@",path] ofType:nil]                   

if ([filePath hasSuffix:@"js"]) {                       

[header_HTML appendFormat:@"<script src=\"%@\"><\script>",filePath]                   

}else if ([filePath hasSuffix:@"css"]){                        

[header_HTML appendFormat:@"<link rel=\"stylesheet\"               

[_webView loadHTMLString:header_HTML baseURL:[[NSBundle mainBundle] bundleURL]]            

       }        

}