美文网首页UI基础iOS小超人
iOS 获取UIWebView完整高度

iOS 获取UIWebView完整高度

作者: KKWong | 来源:发表于2016-03-16 14:55 被阅读6868次

如今,移动APP与网页交互的关系越来越密切,所以,在APP开发的过程中,会经常使用到UIWebView.在我自己之前做的一个项目,也出现了APP与网页交互的功能,需要使用webview把一段html加载出来,然后通过计算webview的高度去设置其他控件的位置。然而,在获取webview高度的时候,出现了webview高度获取不完全的情况,而这种情况的出现都是在html有图片的情况下,因为图片还没有加载完,高度就已经计算完。当时卡在这里很久,也尝试过很多方法,但高度都还是会获取不完整,最后,通过监听webview内容的变化,来重新计算webview的高度,这样就获取了webview的完整高度。

UIWebView UIWebView

#import "FirstViewController.h"

#import "SecondViewController.h"@interface FirstViewController ()

@property (nonatomic, strong) UIWebView *showWebView;

@property (nonatomic, assign) float webViewHeight;

@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation FirstViewController

- (void)viewDidLoad {    

[super viewDidLoad];    

self.title = @"FIRST VC";    

self.view.backgroundColor = [UIColor greenColor];        

//设置uiwebview要加载的数据源,由于直接黏贴数据源会自动识别里面的URL,所以只能贴图

html数据源

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];

self.scrollView.backgroundColor = [UIColor redColor];

self.scrollView.contentSize = CGSizeMake(375, 1);

[self.view addSubview:self.scrollView];

self.showWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 375, 1)];

self.showWebView.delegate = self;

self.showWebView.userInteractionEnabled = NO;

[self.scrollView addSubview:self.showWebView];

//加载数据源

[self.showWebView loadHTMLString:htmlStr baseURL:nil];

//监听webview

[self.showWebView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];

//监听触发

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

{

if ([keyPath isEqualToString:@"contentSize"]) {

//通过JS代码获取webview的内容高度

self.webViewHeight = [[self.showWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];

//通过webview的contentSize获取内容高度

//        self.webViewHeight = [self.showWebView.scrollView contentSize].height;

CGRect newFrame = self.showWebView.frame;

newFrame.size.height  = self.webViewHeight;

NSLog(@"-document.body.scrollHeight-----%f",self.webViewHeight);

NSLog(@"-contentSize-----%f",self.webViewHeight);

[self createBtn];

self.showWebView.frame = CGRectMake(0, 0, 375, self.webViewHeight);

}

}

//移除观察者

-(void)dealloc

{

[self.showWebView.scrollView removeObserver:self

forKeyPath:@"contentSize" context:nil];

}

- (void)createBtn

{

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(0, self.webViewHeight, 375, 50);

btn.backgroundColor = [UIColor lightGrayColor];

[btn setTitle:@"PUSH TO B" forState:UIControlStateNormal];

[btn addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];

[self.scrollView addSubview:btn];

self.scrollView.contentSize = CGSizeMake(375, self.webViewHeight+50);

}

- (void)push{

SecondViewController *svc = [[SecondViewController alloc] init];

[self.navigationController pushViewController:svc animated:YES];

}

@end

然后我们看看通过document.body.scrollHeight和[self.showWebView.scrollView contentSize].height获取的高度

document.body.scrollHeight [self.showWebView.scrollView contentSize].height

通过输出可以看到,这两个方法获取webview的高度完全一致.

相关文章

网友评论

  • 鬼崇祟:楼主有没有遇到计算出高度之后,高度过高,刷新table的时候,内存爆炸直接崩了
  • 阶前梧叶:KVO contentSize 计算webview 高度,如果页面内容很长,会crash
    SuperDanny:目前我也是遇到这个问题,大家是否有好点idea
    阶前梧叶:KVO 监听contentSize设置一个抖动范围,减少刷新频率,一定程度上能缓解。
    但是这个问题其实从根本上解决不了:
    网页过大,那么webview完全展示,高度就会过高,绘制的区域就很大,总有一个超过内存的点,超过则crash
    05ee4e8762a8:有什么好的解决方案吗?
  • 无尽思绪:这个js获取到的高度可是2000多, 完全不能直接赋值到oc里面。
    js高度和oc单位都不一样。
  • 773f0db0e614:为毛这个按钮 在界面上有多个?
    KKWong:@LC_ 你是说tabbar吗? 那跟webview没有任何关系,你可以忽略!

本文标题:iOS 获取UIWebView完整高度

本文链接:https://www.haomeiwen.com/subject/pleqlttx.html