通过-(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