app嵌套web网页滚动流畅

1.总体思路就是webview套在tableview上边,特别注意要设置webview的scrollview属性scrollenabled为no。其实是个体验上的假象。

2.如果html需要vue渲染,这时候就不能获取整个网页的高度。解决方法,可以使用桥接,利用异步操作,加载完链接之后,html端调起桥接方法,iOS端取得高度后刷新tableview

//html高度获取不完整的解决方法
-(void)catchJsHeight
{
    [self.bridge registerHandler:@"command" handler:^(id data, WVJBResponseCallback responseCallback) {
        //获取到webview的高度
        //        CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];//
        CGFloat height = [data[@"height"] floatValue];
//        NSLog(@"webheight==%f",height);
        self.webView.frame = CGRectMake(self.webView.frame.origin.x,self.webView.frame.origin.y, SCREEN_WIDTH, height);
        [self.payTableView reloadData];
//        if (responseCallback) {
//
//        }
    }];
}

catchJsHeight方法在创建webview之后调用


3.上代码

//
//  ViewController.m
//  dynamicWebView
//
//  Created by ss on 2016/11/21.
//  Copyright © 2016年 ss. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIWebViewDelegate, UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UIWebView * contentWebView;
@property (nonatomic,strong) UITableView * payTableView;
@end

@implementation ViewController


-(UIWebView *)contentWebView
{
    if (!_contentWebView) {
        _contentWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 1)];
        _contentWebView.delegate = self;
        //预先加载url
        [_contentWebView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://shop.us-app.com/static_html/feature/subsunglass.html"]]];
        
//         [_contentWebView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://hotr.us-app.com/share/massage.html?id=21"]]];
        
        _contentWebView.backgroundColor = [UIColor yellowColor];
        _contentWebView.scrollView.scrollEnabled = NO;
    }
    return _contentWebView;
}


-(UITableView *)payTableView
{
    if (!_payTableView) {
        CGRect childFrame = CGRectMake(0, 64, 320, 468);
        _payTableView = [[UITableView alloc]initWithFrame:childFrame];
        _payTableView.backgroundColor = [UIColor whiteColor];
        _payTableView.delegate = self;
        _payTableView.dataSource = self;
        _payTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _payTableView.showsVerticalScrollIndicator = NO;
//        _payTableView.bounces = NO;
        CGRect frame = _payTableView.frame;
        frame.origin.y = 64;
        _payTableView.frame = frame;
    }
    
    return _payTableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.payTableView];
    self.view.backgroundColor = [UIColor yellowColor];
    
    
}

#pragma mark - TableViewDelegate & TableViewDatasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  1;
}

- (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.contentView addSubview:self.contentWebView];
            /* 忽略点击效果 */
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
        return cell;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
        /* 通过webview代理获取到内容高度后,将内容高度设置为cell的高 */
        NSLog(@"cellheight==%f",self.contentWebView.frame.size.height);
        return self.contentWebView.frame.size.height;
   
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - UIWebView Delegate Methods
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //获取到webview的高度
    CGFloat height = [[self.contentWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];//@"document.body.offsetHeight"
    NSLog(@"webheight==%f",height);
    self.contentWebView.frame = CGRectMake(self.contentWebView.frame.origin.x,self.contentWebView.frame.origin.y, 320, height);
    [self.payTableView reloadData];
}



- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"webViewDidStartLoad");
}

//-(void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    [self.payTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
//}
//
//-(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
//{
//    [self.payTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
//}


@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值