动态获取UIWebView的高度

本文详细介绍了如何通过实现UIWebViewDelegate协议来动态获取网页加载后的高度,并且结合实例展示了如何利用此特性进行网页内容的布局优化。通过实例演示了如何在网页加载完成后调整滚动视图的垂直滚动范围,实现网页内容的精确展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过-(void)webViewDidFinishLoad:(UIWebView *)webView 这个方法可以动态的获取到其webview的高度,但是要声明UIWebViewDelegate协议。

#import "GoodDetail.h"

#import "Good.h"
#import "UIImageView+AsyncImage.h"
@interface GoodDetail ()<UIWebViewDelegate>

@property (nonatomic, strong) UIImageView *imageview ;

@property (nonatomic, strong)UIWebView *webView ;

@property (nonatomic, strong)UILabel *num;
@end

@implementation GoodDetail

-(void)viewDidLoad{

    NSString *pth = [NSString stringWithFormat:@"%@%@",IP,_good.picture];

    NSURL *url = [NSURL URLWithString:pth];

   UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

   _imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 150)];       

    //以上4行代码  是就NSString 的字符串路径,将网络图片在_imageview中显示  

    //[_imageview setImageForUrl:pth];  
    _imageview.image = image;

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(63, 245, 120, 20)];

    view.layer.borderColor =  [UIColor greenColor].CGColor;

    view.layer.borderWidth = 1;

    UIButton *jian = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
 
    jian.tag = 1;
 
   [jian addTarget:self action:@selector(jian:) forControlEvents:UIControlEventTouchUpInside];

    [jian setTitle:@"➖" forState:UIControlStateNormal];

    UIButton *add = [[UIButton alloc]initWithFrame:CGRectMake(80, 0, 40, 20)];

    [add addTarget:self action:@selector(jian:) forControlEvents:UIControlEventTouchUpInside];

    add.tag = 2;

    [add setTitle:@"➕" forState:UIControlStateNormal];

     _num= [[UILabel alloc]initWithFrame:CGRectMake(40, 0, 60, 20)];

    _num.text = @"1";

    _num.textAlignment = NSTextAlignmentCenter;

    [view addSubview:jian];

    [view addSubview:add];

    [view addSubview:_num];

    _logistic= [[UILabel alloc]initWithFrame:CGRectMake(3, 270, kWidth, 45)];

    _logistic.text = [NSString stringWithFormat:@"物流备注:%@",_good.logistics];

    _logistic.numberOfLines = 0;

   
_webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 325, kWidth, 200)];

    NSURL *weburl = [NSURL URLWithString:_good.url];

    [_webView loadRequest:[NSURLRequest requestWithURL:weburl]];

    _webView.backgroundColor = [UIColor redColor];

//设置webview本身不可以滚动

    _webView.scrollView.scrollEnabled = NO;

//必须声明webview的协议才能动态的获取其大小

    _webView.delegate = self;

     _scrollview= [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 49)];

    // = CGSizeMake(kWidth, 800);
 
   [_scrollview addSubview:_imageview];

   [_scrollview addSubview:view];

   [_scrollview addSubview:_webView];

   
    UIView *bottom = [[UIView alloc]initWithFrame:CGRectMake(0,kHeight -49 , kWidth, 49)];

    bottom.backgroundColor = [UIColor whiteColor];

    UIButton *shop = [[ UIButton alloc]initWithFrame:CGRectMake(3, 3, (kWidth - 11)/2, 43)];

    shop.backgroundColor = [UIColor redColor];

    [shop setTitle:@"下单购买" forState:UIControlStateNormal];

    UIButton *addshopcar = [[ UIButton alloc]initWithFrame:CGRectMake((kWidth - 11)/2 +8, 3, (kWidth - 11)/2, 43)];

    addshopcar.backgroundColor = [UIColor redColor];

    [addshopcar setTitle:@"加入购物车" forState:UIControlStateNormal];

    [bottom addSubview:shop];

    [bottom addSubview:addshopcar];

    [self.view addSubview:_scrollview];

    [self.view addSubview:bottom];

    self.view.backgroundColor = [UIColor whiteColor];
}

-(void)jian:(UIButton *)btn{

    NSInteger i =  _num.text.intValue;

   
    if(1==btn.tag){

        if(1 == i){

            return;

        }

        i--;

       
    }else{

        if(999==i){

            return;

        }

        i++;

    }

    _num.text = [NSString stringWithFormat:@"%lu",i];

   
}

//获取webView的高度   

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    CGSize size = [webView sizeThatFits:webView.bounds.size];

    CGRect frame = webView.frame;

    frame.size = size;

    webView.frame = frame;

//动态设置scrollview的垂直滚动范围

    _scrollview.contentSize = CGSizeMake(0, 325+size.height); 
}


@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值