1、适用于网页内容:
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *meta = [NSStringstringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"", webView.frame.size.width];
[webView stringByEvaluatingJavaScriptFromString:meta];
}
备注:
如果文件中含有图片,设置自适应屏幕之后,会出现图片不能充满整个webview的情况。此时可以用一个不建议使用的恶心方法:
// webView.scalesPageToFit=YES;
introduction=[introduction stringByReplacingOccurrencesOfString:@"<img " withString:@"<img width=\"312\""];
[webView loadHTMLString:introduction baseURL:nil];
2、适用于单独的图片:
UITableView *tableViewMain = [[UITableViewalloc]init];
CGRect rect = self.view.frame;
NSLog(@"height:%f, width:%f",rect.size.height,rect.size.width);
tableViewMain.frame = rect;
[tableViewMain setSeparatorStyle:UITableViewCellSeparatorStyleNone];
NSURL *url = [NSURLURLWithString:@"http://image.demo.shopxx.net/4.0/201501/65feeba6-e840-486d-a617-7ebe77d1b244.png"];
UIImage *image = [UIImageimageWithData: [NSDatadataWithContentsOfURL:url]];
float height=(rect.size.width/image.size.width)*image.size.height;
UIImageView *imageView=[[UIImageViewalloc] initWithFrame:CGRectMake(0,0, rect.size.width, height)];
imageView.backgroundImage=image;
[tableViewMain addSubview:imageView];
[self.view addSubview:tableViewMain];
//绑定事件放到最后
tableViewMain.delegate = self;
tableViewMain.dataSource = self;