一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

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:@"http://www.111com.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

热门栏目