2016-07-05 13 views
1

セル内にUIWebViewを持つカスタムUITableViewCellがあります。 UIWebViewにはデフォルトの高さ制約(20ピクセル)があります。ロード後にUITableViewCellの高さを変更します。

class TableViewCell: UITableViewCell, UIWebViewDelegate { 

    @IBOutlet weak var webView: UIWebView! 
    @IBOutlet weak var webViewHeight: NSLayoutConstraint! 

    func webViewDidFinishLoad(webView: UIWebView) { 

     webView.frame.size.height = 1 
     var contentSize = webView.scrollView.contentSize 

     webView.frame.size.height = contentSize.height 
     webView.scrollView.frame.size.height = contentSize.height 

     webViewHeight.constant = contentSize.height 

    } 
} 

次に、私はWebViewにロードしたいものを設定します。 UIWebViewDelegateをロードした後にwebViewDidFinishLoadを呼び出し、高さの制約を変更します。そして、現時点では、デバッグ領域に制約があります。

2016-07-06 09:11:20.492 TEST CELL[1746:56476] Unable to simultaneously satisfy constraints. 
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
(
"<NSLayoutConstraint:0x7fbc087088f0 V:[UIWebView:0x7fbc0871eb70(50)]>", 
"<NSLayoutConstraint:0x7fbc086b46f0 UIWebView:0x7fbc0871eb70.top == UITableViewCellContentView:0x7fbc0871dea0.topMargin>", 
"<NSLayoutConstraint:0x7fbc086b4740 UITableViewCellContentView:0x7fbc0871dea0.bottomMargin == UIWebView:0x7fbc0871eb70.bottom>", 
"<NSLayoutConstraint:0x7fbc08700c10 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fbc0871dea0(59)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fbc087088f0 V:[UIWebView:0x7fbc0871eb70(50)]> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

ロード後に新しい制約定数を正しく設定する方法はありますか?

+0

制約に関して表示されるエラーは何ですか? – SArnab

+0

エラー内容を編集した質問内容を編集しました。 –

+0

私はあなたの問題を解決することができます。どこからでも59 pxの制約がありますか?削除してみてください...また、[cell layoutIfNeeded]を書くことを忘れないでください。制約に新しい値を割り当てた後。 –

答えて

0

webViewDidFinishLoadが何度も呼び出すことができ、すべての時間は、フレームはあなたがこのプロパティを確認する必要がありますロード行われます

if (webview.isLoading) { 
    return 
} else { 
    // do my stuff 
} 
0

あなたはTableViewCellに制約をトップにwebViewを固定し、下でこれを達成し、その後にすることができますこのセルはで表示されUITableViewであなたのクラスは、

tableView.rowHeight = UITableViewAutomaticDimension 

を設定し、012に制約を行っているようTableViewCellwebViewの高さを設定します。

これはセルに正しい高さを与えます。最後に、デリゲートパターンを使用してUITableViewを呼び出してリロードすることができます(おそらく、このセルのindexPathをリロードします)。

+0

もちろん、 'tableView.rowHeight = UITableViewAutomaticDimension'を設定しました。 'func webViewDidFinishLoad'では、すべてのwebViewフレーム(編集された質問。Up)と制約の高さを設定しました。私が 'tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow:0、inSection:0)]、withRowAnimation:UITableViewRowAnimation.Noneをリロードしようとすると' webViewが再初期化されました。 –

関連する問題