2013-04-24 82 views
2

我有一個問題。在Xcode我有一個ViewController嵌入查看>ScrollView和滾動查看內部是一個UIWebView。iOS:調整UIWebView的高度 - 奇怪的行爲

該應用程序讀取一些XML數據並將格式化後的文本粘貼到UIWebView中。在UIWebView-Element是另一個元素之後,所以我必須動態調整UIWebView的高度。

加載視圖後,一切都很好,但是當我點擊屏幕時,視圖將scrollview重新設置爲其故事板高度。

爲什麼?

- (void)webViewDidFinishLoad:(UIWebView *)aWebView 
{ 
aWebView.scrollView.scrollEnabled = NO; // Property available in iOS 5.0 and later 
CGRect frame = aWebView.frame; 

frame.size.width = 280;  // Your desired width here. 
frame.size.height = 1;  // Set the height to a small one. 

aWebView.frame = frame;  // Set webView's Frame, forcing the Layout of its embedded  scrollView with current Frame's constraints (Width set above). 

frame.size.height = aWebView.scrollView.contentSize.height; // Get the corresponding   height from the webView's embedded scrollView. 

aWebView.frame = frame;  // Set the scrollView contentHeight back to the frame itself. 

NSLog(@"size: %f, %f", aWebView.frame.size.width, aWebView.frame.size.height); 

// Position URL Label 
CGRect frame_label = fachmesseDetailWebsite.frame; 
NSLog(@"x: %f", frame_label.origin.x); 
NSLog(@"y: %f", frame_label.origin.y); 
frame_label.origin.y=aWebView.frame.size.height+115;//pass the cordinate which you want 
frame_label.origin.x= 20;//pass the cordinate which you want 
fachmesseDetailWebsite.frame= frame_label; 
NSLog(@"x: %f", frame_label.origin.x); 
NSLog(@"y: %f", frame_label.origin.y); 

} 

回答

1

每當你改變幀,你必須重新加載網頁視圖則只有網頁流量框會不管你放在那裏的其他明智它不影響網頁流量。

[webview reload]; 

編輯: 這一項工作

UIWebView *web=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; 

    NSString* html = [NSString stringWithFormat:@"<html><head><style>a { color: #db5226; text-decoration: none; font-weight: bold; } body { padding: 0; margin: 0; font-size:45px;font-family: Helvetica;text-align:left; color:black; } </style></head<body>%@</body></html>",@"helow"]; 
    [web loadHTMLString:html baseURL:nil]; 
    [self.view addSubview:web]; 
+0

謝謝。下面是我如何將內容添加到web視圖: 'NSString * html = [NSString stringWithFormat:@「%@「,item.description]; [self.fachmesseDetailText loadHTMLString:html baseURL:nil];' 當我重新加載webview時,everythink是空白的... – 2013-04-24 07:34:19

+0

請參閱本教程http://www.roseindia.net/answers/viewqa/Mobile-Applications/16673 -UIWebview-load-url.html – Balu 2013-04-24 07:36:54

+0

一旦檢查它.. – Balu 2013-04-24 07:46:56