2016-04-02 7 views
1

の高さを取得し、私は(彼らのNSLayoutConstraint sの)次の階層を持っているUIViewControllerを持っている:UIScrollViewのcontentView

UIView 
-UIScrollView 
--UIView-ContentView (zero margins to the UIScrollview and equal height to UIScrollVIew's parent; mentioned in the code as self.contentView) 
---UIView (fixed height, 0 top margin to Content View) 
---UIView (fixed height, 0 top margin to above view) 
---UIView (fixed height, 0 top margin to above view) 
---UIView (fixed height, 0 top margin to above view) 
---UIWebView (fixed height & linked to IBOutlet in the class; gets expanded) 
---UILabel (top margin to UIWebView) 
---UIView (fixed height, top margin to above label) 
---UIView (fixed height, 0 top margin to above view) 
---UIView (fixed height, 0 top margin to above view) 
---UIView (fixed height, 0 top margin to above view) 
---UIButton (fixed height, top margin to above view) 

Scenario: 
1. On viewDidLoad() I'm adding the HTML text by appending the JavaScript code that returns the height of the UIWebView 
2. on UIWebViewDelegate method (shouldStartLoadWithRequest) I'm setting the following things: 
- webViewHeight.constant (NSLayoutConstraint, linked from IB) 
- self.scrollView.contentSize = CGSizeMake(self.view.frame.width, CGRectGetHeight(self.contentView.frame)) 


override func viewDidLoad() { 
    self.webView.scrollView.scrollEnabled = false 

    let heightHackScript = "<script type='text/javascript'>window.onload = function() { window.location.href = 'ready://' + document.body.offsetHeight; }</script>" 

     if let cBody:NSData = self.model?.text_content { 

      self.webView.loadHTMLString(heightHackScript + String(data: cBody, encoding: NSUTF8StringEncoding)!, baseURL: nil) 
     } 
    } 


func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { 

    let url = request.URL 

    if navigationType == .Other { 
    if url?.scheme == "ready" { 
     webViewHeight.constant = (url?.host?.floatValue)! 
     self.scrollView.contentSize = CGSizeMake(self.view.frame.width, CGRectGetHeight(self.contentView.frame)) 
     return false 
    } 
    } 
    return true 
} 

問題がCGRectGetHeight(self.contentView.frame)が所望の高さを取得していないということです。何か案は?

私は、HTMLがUIWebViewにロードされた後に、ビューの制約で高さを取得したいと思います。

答えて

0

それはコンテンツだ収まるようにWeb表示のサイズを変更しようとしている困難な問題は、Webビュー自体は、DOMのコンテンツがあるかもしれないそれの内側に表示されているどのように背の高い任意のアイデアを持っていないという事実です。あなたがしなければならないのは、JavaScriptを使ってコンテンツの内容をDOMに問い合わせることです。

[webView stringByEvaluatingJavaScriptFromString: @"document.height"] 

またはスウィフト中:

webView.stringByEvaluatingJavaScriptFromString("document.height") 

そして、我々のUIWebViewのサイズを選択するために、結果の値を使用して我々のコードでは、我々はそれをこのような何かを行います。

+0

感謝しかし、私は取得したいのですが、 'UIViewの-ContentView(UIScrollViewのとUIScrollViewのの親と同じ高さにゼロ余白; self.contentViewように、コードに記載された)の全高である高さに基づいて、' UIScrollViewのContentSizeの高さを更新します。 –