ios - 加载后更改 UITableViewCell 高度

标签 ios swift uitableview autolayout

我在单元格内有自定义 UITableViewCell 和 UIWebView。 UIWebView 有默认的高度限制(20 像素)。

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.

如何在加载后正确设置新的约束常量?

最佳答案

webViewDidFinishLoad 可以调用多次,每次加载完一个帧,所以你必须检查这个属性:

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

关于ios - 加载后更改 UITableViewCell 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38206617/

相关文章:

ios - NSManagedObject 错误/无法获取 NSManagedObject 处理的 'primitive' 对象

ios - 比较 UIViewController 实例

ios - UITableViewCell 内的 TableView - 必须注册一个 nib 或类

ios - 由于核心数据迁移,应用程序在从应用程序商店更新后崩溃

ios - 如何将一个 TableView 放入另一个 TableView 的单元格中?任何想法?

ios - 自动布局自动调整大小的单元格和 UITableView 口吃/抖动

iphone - iPhone 上的 SQLite 表无效?

ios - iOS 设备 UDID 区分大小写吗?

ios - (Rubymotion) 如何在 Formotion 中执行此自定义 View ?

swift - 将 Revmob 集成到 SpriteKit Swift 游戏中