【发布时间】:2017-04-18 01:48:11
【问题描述】:
我已经遇到这个问题几天了,但仍然没有找到解决方法。基本上我正在尝试创建一个带有自定尺寸单元的UITableView。我已经使用UITableViewAutomaticDimension 将单元格的高度设置为自动。单元格有一个高度约束来测试我的代码,因为这个约束应该确定单元格的高度。这是我对单元格的限制:
let views = ["view": view]
self.addConstraints(NSLayoutConstraint.constraints(
withVisualFormat: "H:|[view]|",
options: [],
metrics: nil,
views: views
))
self.addConstraints(NSLayoutConstraint.constraints(
withVisualFormat: "V:|[view]|",
options: [],
metrics: nil,
views: views
))
NSLayoutConstraint(item: contentView, attribute: .width, relatedBy: .equal, toItem: self.superview!, attribute: .width, multiplier: 1, constant: 0).isActive = true
这是我收到的错误消息:
2017-04-17 22:40:01.596805-0300 ViraVira-Info[52208:1001696] [LayoutConstraints] 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.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x600000099190 h=--& v=--& UITableViewCellContentView:0x7f987070f5f0.height == 44.5 (active)>",
"<NSLayoutConstraint:0x6000000966c0 UIView:0x7f9870709be0.height == 100 (active)>",
"<NSLayoutConstraint:0x600000098dd0 V:|-(0)-[UIView:0x7f9870709be0] (active, names: '|':UITableViewCellContentView:0x7f987070f5f0 )>",
"<NSLayoutConstraint:0x600000098e70 V:[UIView:0x7f9870709be0]-(0)-| (active, names: '|':UITableViewCellContentView:0x7f987070f5f0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000000966c0 UIView:0x7f9870709be0.height == 100 (active)>
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.
如您所见,tableview 会自动创建一个 height constraint,这与我的其他高度限制冲突。
"<NSAutoresizingMaskLayoutConstraint:0x600000099190 h=--& v=--& UITableViewCellContentView:0x7f987070f5f0.height == 44.5 (active)>"
但我找不到删除或阻止此约束发生的方法。我已经尝试将内容视图的translatesAutoresizingMask 设置为false,将单元格视图设置为自身以及将UIView 添加到内容视图中,但没有成功。这样做给了我以下错误:
Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a `tableview cell's` content view. We're considering the collapse unintentional and using standard height instead.
请询问任何其他信息,我很乐意将其添加到此问题中。
【问题讨论】:
-
view.translatesAutoresizingMaskIntoConstraints = false 应该有助于@J.Paravicini
-
你红了这个问题吗?我说我已经尝试过了,但没有成功:/
-
在 cell.contentView.translatesAutoresizingMaskIntoConstraints 上试试
-
@J.Paravicini 如果设置所有 view.translatesAutoresizingMaskIntoConstraints = false 则还有其他视图
-
@NazmulHasan 我尝试将所有视图和子视图中的 translatesAutoresizingMaskIntoConstraints 设置为关闭,但在控制台中出现以下错误:仅警告一次:检测到约束模糊地建议高度为零的情况tableview 单元格的内容视图。我们正在考虑意外折叠并改用标准高度。
标签: ios swift uitableview dynamic constraints