最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
ios开发之UIWebView自适应高度例子
时间:2022-06-25 23:39:23 编辑:袖梨 来源:一聚教程网
UIWebView使用时当无法确切的设置一个固定的高度时,就需要根据网页内容来自适应高度了,参考代码如下:
//
// ViewController.m
// UIwebView高度自适应
//
// Created by mac on 16/3/18.
// Copyright © 2016年 ZMIT. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
UIWebView *myWebView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor grayColor];
myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 30)];
myWebView.delegate = self;
myWebView.scrollView.delegate = self;
myWebView.scalesPageToFit = YES;//自动对页面进行缩放以适应屏幕
[self.view addSubview:myWebView];
NSURL *url = [NSURL URLWithString:@"https://www.111cn.net/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:request];
}
#pragma mark - 代理方法
-(void)webViewDidFinishLoad:(UIWebView *)webView{
CGFloat webViewdocument.body.offsetHeight"]floatValue];
CGRect newFrame = webView.frame;
newFrame.size.height = webViewHeight;
webView.frame = newFrame;
//重新设置webView的contentSize
myWebView.scrollView.contentSize = newFrame.size;
//如果WebView块下面还有其他的控件,可在此处根据myWebView设置其frame
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章
- 无限暖暖神秘作者任务怎么做 神秘作者任务流程攻略 09-18
- 如鸢董奉怎么玩 董奉玩法攻略 09-18
- 嘟嘟脸恶作剧活泼队怎么搭配 活泼队搭配推荐 09-18
- 如鸢曹植值得抽吗 曹植抽取建议 09-18
- 如鸢主线9-13王允低练度怎么过 主线9-13王允低练度过关攻略 09-18
- 嘟嘟脸恶作剧薇尔薇特角色如何 09-18