zoukankan      html  css  js  c++  java
  • iOS WKWebView 获取内容高度 去掉pdf页码

    最近遇到使用UITableView嵌套WKWebView的场景,
    实现原理:禁用WKWebView的滑动功能,在WKWebView加载完后,得到高度,并更改WKWebView的frame,,然后将WKWebView作为tableview的cell展示

    1.获取内容高度

    在得到WKWebView的内容具体高度的实现中,有多种方案,比如KVC监听WKWebView里的scrollview的contentsize,,,但发现这个方法在iPhoneX等高端机型和新系统中,会失效,于是用了另一种方法:代理

    WKWebView 的WKNavigationDelegate代理有个方法
    // 页面内容加载完成之后调用

    - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    }


    注意了,这个只是WKWebView的内容加载完,,,就像刚把文件下载成功,,,还没有真的显示到WKWebView里面,所以这个时候获取的webView.scrollView.contentSize.height为0;

    解决方案:延迟获取

    - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
        [self performSelector:@selector(changeWebViewHeight) withObject:nil afterDelay:0.5f];
    }
    
    -(void)changeWebViewHeight{
    [self hideLableForView:self.webView];
        self.webView.frame = CGRectMake(0, 0,self.view.bounds.size.width,self.webView.scrollView.contentSize.height);
        [self.tableView reloadData];
        [LoadingView closeLoading];//结束加载
    }

    像这样写,就能准确的获取到加载到界面的contentSize,,,具体延迟时间,可以根据内容大小增加或减少

    2.去除页码

    因为我们是在加载本地的pdf文件,发现改了webview的frame后,左上角的页码显示不是从1开始,,,,,于是想改这个页码,没找到改的地方,,,于是就想着屏蔽,,,

    重点:在内容加载到webview里面的界面后!,这时页数已经定下,,,页码lable已经显示出来,就可以影藏他,,,,一定要在我们的延迟方法后,,在代理方法里面影藏都不行,可能那个时候lab还没加上,亲测早一点都不行。

    影藏方法:因为没有具体的API和入口供我们操控,,,再加上通过运行时的界面层级查看,发现整个webview里面只有这个显示页码的控件是UILabel,于是使用简单粗暴的方法:屏蔽调webview里的UILabel控件,通过下面的遍历方法就可实现

    -(void)hideLableForView:(UIView*)view{
        for (UIView * subView in view.subviews) {
            if([subView isKindOfClass:[UILabel class]]){
                subView.hidden = YES;
            }else{
                [self hideLableForView:subView];
            }
        }
    }

    下面是具体demo的代码,

    #import "ViewController.h"
    #import <WebKit/WebKit.h>
    
    @interface ViewController ()<WKNavigationDelegate,WKUIDelegate,UITableViewDelegate,UITableViewDataSource>
    @property (nonatomic, strong) UITableView *tableView;
    @property (strong, nonatomic) WKWebView *webView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
    
           UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
           btn.backgroundColor = [UIColor redColor];
           [btn addTarget:self action:@selector(tap) forControlEvents:UIControlEventTouchUpInside];
           [self.view addSubview:btn];
     }
    -(void)tap{
            self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
            _tableView.dataSource = self;
            _tableView.delegate = self;
            self.tableView.showsHorizontalScrollIndicator = NO;
            self.tableView.showsVerticalScrollIndicator = NO;
            self.tableView.separatorStyle = 0;
            
    
            _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 1)];
            self.webView.navigationDelegate = self;
            self.webView.UIDelegate = self;
            self.webView.scrollView.scrollEnabled = NO;
            NSString *filepath = [[NSBundle mainBundle] pathForResource:@"AppleUpReadMe" ofType:@"pdf"];
            [self.webView loadFileURL:[NSURL fileURLWithPath:filepath] allowingReadAccessToURL:[NSURL fileURLWithPath:filepath]];
            
            
            [self.view addSubview:_tableView];
            
        }
    
    
    // 页面加载完成之后调用
    - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
        [self performSelector:@selector(changeWebViewHeight) withObject:nil afterDelay:0.5f];
    }
    
    
    -(void)hideLableForView:(UIView*)view{
        for (UIView * subView in view.subviews) {
            if([subView isKindOfClass:[UILabel class]]){
                subView.hidden = YES;
            }else{
                [self hideLableForView:subView];
            }
        }
    }
    
    -(void)changeWebViewHeight{
        [self hideLableForView:self.webView];
        self.webView.frame = CGRectMake(0, 0,self.view.bounds.size.width,self.webView.scrollView.contentSize.height);
        [self.tableView reloadData];
    }
    
    
    
    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
        return [[UIView alloc] init];
    }
    
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
         return [[UIView alloc] init];
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return 1;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        return 0.01f;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 0.01f;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
            static NSString *identifier = @"cell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (!cell){
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            }
            [cell.contentView addSubview:_webView];
            return cell;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
    {
        return _webView.frame.size.height;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }

    点个赞再走呗。。。

    如有疑问,联系作者 

    博客园:这个我不知道诶

  • 相关阅读:
    【bzoj1116】[POI2008]CLO 并查集
    【bzoj3831】[Poi2014]Little Bird 单调队列优化dp
    【bzoj4726】[POI2017]Sabota? 树形dp
    【bzoj4822/bzoj1935】[Cqoi2017]老C的任务/[Shoi2007]Tree 园丁的烦恼 树状数组
    【bzoj4884】[Lydsy2017年5月月赛]太空猫 dp
    【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分
    【bzoj2424】[HAOI2010]订货 费用流
    【bzoj2662】[BeiJing wc2012]冻结 分层图Spfa
    【bzoj1195】[HNOI2006]最短母串 AC自动机+状态压缩+BFS最短路
    【bzoj4818】[Sdoi2017]序列计数 矩阵乘法
  • 原文地址:https://www.cnblogs.com/Yongersblog/p/13231594.html
Copyright © 2011-2022 走看看