获取UIWebview的高度4种方法

来源:互联网 发布:p城堡垒优化助手 编辑:程序博客网 时间:2024/04/30 07:14

第一:效果的对比

   

第二、代码区

/*

   UIWebView IOS开发常用的类。它简单好用,使用它,开发者不用写太多的代码。但是,App开发中,获取webview的高度成为开发者必备的知识。我知道网上也有好多方法。我这里是做一个评价,不是新的东西。如有雷同请包涵。

 */

//  ViewController.m

//  获取UIWebview的高度4种方法

//

//  Created by MAC on 16/10/8.

//  Copyright © 2016 NetworkCode小贱. All rights reserved.

//


#import "ViewController.h"

#import "testViewController.h"


const NSString * BaseIpString =@"http://testapi.anyitou.com";

@interface ViewController ()<UIWebViewDelegate>{

    UIWebView * ReloadWebView;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    /* 我们要UIWebview的高度只适应*/

    CGFloat ScreenWeight = [UIScreenmainScreen].bounds.size.width;

    CGFloat ScreenHeight = [UIScreenmainScreen].bounds.size.height;

    UIWebView * WebView = [[UIWebViewalloc]initWithFrame:CGRectMake(10,60, ScreenWeight-20,200)];

    WebView.delegate =self;

    /* 关闭webView的反弹*/

    WebView.scrollView.bounces =NO;

    ReloadWebView = WebView;

    [self.viewaddSubview:WebView];

    NSString * WebviewUrl = [NSStringstringWithFormat:@"%@/resources/Indexintroduce",BaseIpString];

    NSURLRequest * RequestUrl = [NSURLRequestrequestWithURL:[NSURLURLWithString:WebviewUrl]];

    [WebView loadRequest:RequestUrl];

    

    /* 测试按钮*/

    UIButton * Button = [UIButtonbuttonWithType:UIButtonTypeCustom];

    Button.frame =CGRectMake(10, ScreenHeight-50, ScreenWeight-20,40);

    Button.backgroundColor = [UIColormagentaColor];

    [Button setTitle:@"刷新"forState:UIControlStateNormal];

    [Button addTarget:selfaction:@selector(BunClick)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:Button];

    // Do any additional setup after loading the view, typically from a nib.

}

#pragma mark -- btn 的点击事件

-(void)BunClick{

    [ReloadWebViewreload];

    //testViewController * test = [[testViewController alloc]init];

   // [self presentViewController:test animated:YES completion:Nil];

}

#pragma mark ---webview 的代理

-(void)webViewDidStartLoad:(UIWebView *)webView{

    /* 网页的开始加载*/

    NSLog(@"开始");

}

-(void)webViewDidFinishLoad:(UIWebView *)webView{

    /* 网页的加载完成*/

    /* 在网页加载完成时,修改网页的高度,来达到适配*/

    /* 第一种*/

    // [self firstAutoHeight:webView];

    /* 第二种*/

    // [self secondAutoHeight:webView];

    /* 第三种*/

    //[self threeAutoHeight:webView];

    /* 第四种*/

    [selffourAutoHeight:webView];

}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

    /* 网页的加载失败*/

}

/* 是否允许网页的加载*/

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    returnYES;

}

-(void)firstAutoHeight:(UIWebView*)webView{

    /* 网页是含有UIScrollview对象,我们通过获取 webViewScrollvIew的内容的高度来获取webView的高度,同事达到高度适配*/

    CGFloat WebViewHeight = [webView.scrollViewcontentSize].height;

    /* 获取网页现有的frame*/

    CGRect WebViewRect = webView.frame;

    /* 改版WebView的高度*/

    WebViewRect.size.height = WebViewHeight;

    /* 重新设置网页的frame*/

    webView.frame = WebViewRect;

}

-(void)secondAutoHeight:(UIWebView*)webView{

    /* UIWebView的大小先设置为0,然后,使用sizeThatFits获取webview的高度*/

    CGSize WebViewSize = [webViewsizeThatFits:CGSizeZero];

    /* 获取网页现有的frame*/

    CGRect WebViewRect = webView.frame;

    /* 设置高度*/

    WebViewRect.size.height = WebViewSize.height;

    webView.frame = WebViewRect;

    /*

        或者:

            [webView sizeToFit];

            CGRect WebViewRect = webView.frame;

            webView.frame = WebViewRect;

     */

}

-(void)threeAutoHeight:(UIWebView*)webView{

    /* 可取的js:

     1@"document.body.scrollHeight" (为主)

     2@"window.screen.height"要求高度不能超过屏幕高度。

     */

    /* 我们使用js来获取网页的高度*/

    NSString * JsString =@"document.body.offsetHeight";

    CGFloat WebViewHeight = [[webViewstringByEvaluatingJavaScriptFromString:JsString]floatValue];

    /* 获取网页现有的frame*/

    CGRect WebViewRect = webView.frame;

    /* 改版WebView的高度*/

    WebViewRect.size.height = WebViewHeight;

    /* 重新设置网页的frame*/

    webView.frame = WebViewRect;

}

-(void)fourAutoHeight:(UIWebView*)webView{

    /* 设置新的高度*/

    CGFloat WebViewHeight =0.0;

    /* 判断是否有内容存在*/

  //  NSLog(@"%@",[webView subviews]);

    /*

     2016-10-08 14:35:19.057 获取UIWebview的高度[2456:1247177] (

     "<_UIWebViewScrollView: 0x7ff84383b600; frame = (0 0; 355 200); clipsToBounds = YES; autoresize = H; gestureRecognizers = <NSArray: 0x618000054760>; layer = <CALayer: 0x61800003ff40>; contentOffset: {0, 0}; contentSize: {355, 439}>"

     )

     */

    if ([webViewsubviews].count >0) {

        /* 获取最后一个div*/

        UIScrollView * WebViewLastView = [[webViewsubviews] lastObject];

        NSLog(@"%@",[WebViewLastViewsubviews]);

        /*

           输出内容:

         2016-10-08 14:36:47.795 获取UIWebview的高度[2473:1255283] (

         "<UIWebBrowserView: 0x7fbc8980ee00; frame = (0 0; 355 439); text = ' \n\U56fd\U6709\U80cc\U666f \U5b9e\U529b\U4fdd\U969c\n\U8d85\U5f3a\U6297\U98ce\U9669\U80fd\U529b\n \n\U4e13\U4e1a\U7684...'; gestureRecognizers = <NSArray: 0x608000059410>; layer = <UIWebLayer: 0x61800002c680>>",

         "<UIImageView: 0x7fbc8b10d680; frame = (3 194.5; 349 2.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x6000000309a0>> - (null)",

         "<UIImageView: 0x7fbc8b10edf0; frame = (349.5 3; 2.5 194); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x600000030ba0>> - (null)"

         )

         */

        /* 下面有两个方法*/

        /*****************************************************/

        /* 

        /- 第一个 -/

        if ([WebViewLastView isKindOfClass:[NSClassFromString(@"_UIWebViewScrollView") class]]) {

            WebViewHeight = WebViewLastView.contentSize.height;

        }

        /- 获取网页现有的frame -/

        CGRect WebViewRect = webView.frame;

        /- 改版WebView的高度 -/

        WebViewRect.size.height = WebViewHeight;

        /- 重新设置网页的frame -/

        webView.frame = WebViewRect;

        */

        /*****************************************************/

        /* 第二种*/

        if ([WebViewLastViewisKindOfClass:[NSClassFromString(@"_UIWebViewScrollView")class]]) {

            UIView * WebViewLastViewB = [WebViewLastView.subviewsfirstObject];

            if ([WebViewLastViewBisKindOfClass:[NSClassFromString(@"UIWebBrowserView")class]]) {

                WebViewHeight = WebViewLastViewB.frame.size.height;

            }

        }

        /* 获取网页现有的frame*/

        CGRect WebViewRect = webView.frame;

        /* 改版WebView的高度*/

        WebViewRect.size.height = WebViewHeight;

        /* 重新设置网页的frame*/

        webView.frame = WebViewRect;

    }

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end


3、一个网页的层次图


0 0
原创粉丝点击