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];
}

 

點個贊再走唄。。。

如有疑問,聯系作者 

博客園:這個我不知道誒


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM