Objective c 在ScrollView中加载UIWebView时内容发生更改
当用户阅读所选新闻时,我想制作一些类似Pulse news的东西 未加载图像时,尚未指定图像的空间。加载图像后,内容的大小也会改变(文本将被向下推以为图像留出空间) 我该怎么做? 我现在使用的是滚动视图,其中包含uiwebviewObjective c 在ScrollView中加载UIWebView时内容发生更改,objective-c,ios,uiwebview,uiscrollview,Objective C,Ios,Uiwebview,Uiscrollview,当用户阅读所选新闻时,我想制作一些类似Pulse news的东西 未加载图像时,尚未指定图像的空间。加载图像后,内容的大小也会改变(文本将被向下推以为图像留出空间) 我该怎么做? 我现在使用的是滚动视图,其中包含uiwebview - (void)loadView { self.view = [[UIView alloc] initWithFrame:CGRectZero]; UIView *thisView = [[UIView alloc] initWithFrame:CGRectMake(
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:CGRectZero];
UIView *thisView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
contentTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 292, 82)];
contentThumbnailWebView = [[UIWebView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(contentTitleLabel.frame), 292, 0)];
contentDateLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(contentThumbnailWebView.frame), 292, 23)];
playVideoButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(contentThumbnailWebView.frame)/2, CGRectGetMaxY(contentThumbnailWebView.frame)/2, 72, 37)];
[playVideoButton setTitle:@"Play" forState:UIControlStateNormal];
playVideoButton.hidden = YES;
contentSummaryLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(contentDateLabel.frame), 292, 20)];
contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
contentScrollView.backgroundColor = [UIColor whiteColor];
[thisView addSubview:contentScrollView];
[contentScrollView addSubview:contentDateLabel];
[contentScrollView addSubview:contentTitleLabel];
[contentScrollView addSubview:contentThumbnailWebView];
[contentScrollView addSubview:playVideoButton];
[contentScrollView addSubview:contentSummaryLabel];
//[self.view addSubview:thisView];
self.view = thisView;
contentThumbnailWebView.delegate = self;
}
我读过几个关于这个的话题,但我仍然无法解决它
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
fittingSize.width = aWebView.frame.size.width;
frame.size = fittingSize;
aWebView.frame = frame;
NSLog(@"size: %f, %f", fittingSize.width, fittingSize.height);
[contentDateLabel setFrame:CGRectMake(10, CGRectGetMaxY(aWebView.frame), 292, 23)];
playVideoButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(aWebView.frame)/2, CGRectGetMaxY(aWebView.frame)/2, 72, 37)];
[contentSummaryLabel setFrame:CGRectMake(10, CGRectGetMaxY(contentDateLabel.frame), 292, contentSummaryLabel.frame.size.height )];
contentScrollView.contentSize = CGSizeMake(contentScrollView.frame.size.width, CGRectGetMaxX(contentSummaryLabel.frame));
}
我加载了jpg图像链接。图像已加载,但高度未正确调整大小。当webview加载时,我也无法再次滚动scrollView
下面是我调用要加载的图像的时候
- (void) setImageAsThumbnail:(NSString *)imagehumbnailLink
{
NSURL *imageURL = [NSURL URLWithString:imageThumbnailLink];
NSString *cssString = @"<style type='text/css'>img {width: 292px; height: auto;}</style>";
NSString *myHTMLContent = @"<html><head></head><body><img src='%@'></body></html>";
NSString *htmlString = [NSString stringWithFormat:@"%@%@",cssString,myHTMLContent];
NSString *imageHTML = [[NSString alloc] initWithFormat:htmlString, imageURL];
contentThumbnailWebView.scalesPageToFit = YES;
[contentThumbnailWebView loadHTMLString:imageHTML baseURL:nil];
}
-(void)setImageAshumbnail:(NSString*)imagehumbnailLink
{
NSURL*imageURL=[NSURL URLWithString:imageThumbnailLink];
NSString*cssString=@“img{宽度:292px;高度:auto;}”;
NSString*myHTMLContent=@;
NSString*htmlString=[NSString stringWithFormat:@“%@%@”,CSSSSTRING,myHTMLContent];
NSString*imageHTML=[[NSString alloc]initWithFormat:htmlString,imageURL];
contentThumbnailWebView.scalesPageToFit=是;
[contentThumbnailWebView加载HtmlString:ImageHtmlBaseURL:nil];
}
谢谢大家! 我经常在UIScrollView中使用UIWebView,因为加载HTML是异步完成的,所以在加载完成后,必须调整web视图的大小和滚动视图的内容大小
[webView sizeThatFits:CGSizeZero]
对我来说也不管用。相反,在webViewDidFinishLoad:(UIWebView*)webView
中,您可以通过执行一段Javascript来测量HTML文档的大小:
NSString *js = @"document.getElementById('container').offsetHeight;";
NSString *heightString = [webView stringByEvaluatingJavaScriptFromString:js];
int height = [heightString intValue]; // HTML document height in pixels
在本例中,必须将ID“container”分配给HTML中的顶级元素(确保其周围没有边距)。这种方法对我很有效