[英]Implementing UIWebView inside UIScrollView
我正在嘗試在UIScrollView中使用UIWebView,我想在圖像下顯示Webview並使它們可滾動。 因此,我執行了以下步驟:
這是我的代碼:
- (void)viewDidLoad {
[super viewDidLoad];
self.contentReader.scrollView.scrollEnabled = NO;
NSString *myDescriptionHTML = [NSString stringWithFormat:@"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-family: \"%@\"; font-size: %d;}\n"
"</style> \n"
"</head> \n"
"<body>%@</body> \n"
"</html>", @"JF Flat", 18, self.thePost.content];
[self.contentReader loadHTMLString:[NSString stringWithFormat:@"<div style='text-align:right; text-align:justify; direction:rtl'><style type='text/css'>img { max-width: 100%%; width: auto; height: auto; }</style><style type='text/css'>iframe { max-width: 100%%; width: auto; height: auto; }</style>%@<div>",myDescriptionHTML] baseURL:nil];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
// get the html content height
NSString *output = [webView stringByEvaluatingJavaScriptFromString:@"document.height;"];
NSLog(@"Content Size %@",output);
// adjust the height of webView
NSLog(@"WebView Hieght before Update%f", webView.frame.size.height);
CGRect frame = webView.frame;
frame.size.height = 1;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
NSLog(@"WebView Hieght After Update%f", webView.frame.size.height);
[self.scroller setContentSize:webView.bounds.size];
}
這是日志消息:
[5150:203791] WebView Hieght before Update360.000000 [5150:203791] WebView Hieght After Update5888.000000
問題是當我向下滾動時,內容沒有顯示,什么也沒顯示。 檢查圖片:
更新調試視圖Hearachy。
如您所見,視圖層次結構中的WebView高度沒有改變。 僅網頁更改了大小。
我們已經在評論中解決了這個問題,因此為了完整起見,我們只給出這個答案。
如果您使用的是自動布局,請確保通過設置約束(例如高度約束)並更改其常量以更改布局來更改WebView的大小。 如果要設置自動版式更改的動畫,請執行以下操作:
[UIView animateWithDuration:0.5 animations:^{
self.webViewHeightConstraint.constant = fittingSize.height;
[self.view layoutIfNeeded];
}];
有點遠,但您可能想嘗試
webView.scrollView.contentSize = fittingSize;
要么
webView.scrollView.frame = CGRectMake(0,0,fittingSize.width, fittingSize.height);
由於滾動功能已關閉,因此加載html后內容大小或框架可能不會更改。 就像我說的很遠,但可能有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.