我正在尝试以编程方式创建一个 UITableView(即没有 Storyboards/Interface Builder)。我有一个通知列表(一个简单的模型,包含标题、作者和一些内容等数据)。我希望行在垂直方向上动态调整大小(我只发布 iOS >= 8.0,所以 UITableViewAutomaticDimension 是我的行的高度)。现在,行应该非常简单。共有三个 UILabel。每一个都按标题、作者和内容中的一个顺序显示,每个布局在另一个之上;但是,我不断收到警告:“检测到约束模糊地建议高度为零的情况”。我的理解是我的 LayoutConstraints 设置不正确;但是,我已经尝试了所有我能想到的约束组合,但似乎没有任何方法可以修复此警告!
这里是单元格定义:
import UIKit
public class NoticeCellView : UITableViewCell {
public static let NOTICE_CELL_REUSE_IDENTIFIER = "noticeCell"
private let titleLabel = UILabel()
private let authorLabel = UILabel()
private let contentLabel = UILabel()
public func setNotice(notice: Notice) {
titleLabel.text = notice.getTitle()
authorLabel.text = notice.getAuthorFullName()
contentLabel.text = notice.getContent()
}
public override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.contentView.translatesAutoresizingMaskIntoConstraints = false
titleLabel.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(titleLabel)
authorLabel.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(authorLabel)
contentLabel.translatesAutoresizingMaskIntoConstraints = false
contentLabel.numberOfLines = 0;
self.contentView.addSubview(contentLabel)
self.addConstraints([
NSLayoutConstraint(
item: self.contentView,
attribute: .Top,
relatedBy: .Equal,
toItem: titleLabel,
attribute: .Top,
multiplier: 1,
constant: 0
),
NSLayoutConstraint(
item: titleLabel,
attribute: .Bottom,
relatedBy: .Equal,
toItem: authorLabel,
attribute: .Top,
multiplier: 1,
constant: 0
),
NSLayoutConstraint(
item: authorLabel,
attribute: .Bottom,
relatedBy: .Equal,
toItem: contentLabel,
attribute: .Top,
multiplier: 1,
constant: 0
),
NSLayoutConstraint(
item: contentLabel,
attribute: .Bottom,
relatedBy: .Equal,
toItem: self.contentView,
attribute: .Bottom,
multiplier: 1,
constant: 0
),
NSLayoutConstraint(
item: self.contentView,
attribute: .Bottom,
relatedBy: .Equal,
toItem: self,
attribute: .Bottom,
multiplier: 1,
constant: 0
),
NSLayoutConstraint(
item: contentLabel,
attribute: .Width,
relatedBy: .LessThanOrEqual,
toItem: self.contentView,
attribute: .Width,
multiplier: 1,
constant: 0
),
])
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
这是 UITableViewController 代码的相关片段:
public override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(NoticeCellView.classForCoder(), forCellReuseIdentifier: NoticeCellView.NOTICE_CELL_REUSE_IDENTIFIER)
self.tableView.estimatedRowHeight = 200
}
public override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
public override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return TEST_NOTICES.count
}
public override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(NoticeCellView.NOTICE_CELL_REUSE_IDENTIFIER) as! NoticeCellView
cell.setNotice(TEST_NOTICES[indexPath.row])
return cell
}
澄清一下:一切正常( View 布局正确,满足约束条件等),除了未动态设置高度。清除我缺少的一些约束以强制 contentView 扩展到非零高度。在我拔头发之前,请告诉我那个约束是什么。
谢谢!!
最佳答案
总是考虑这样的约束。每个 View 都有四个特征:它的 x 位置、它的 y 位置、它的宽度和它的高度。查看每个 View 并问问自己:我是否提供了足够的信息来确定这些特征的所有四个?如果不是,则说明您的约束不够。
您的约束严重不足。您只关注了高度——垂直维度。没有关于水平维度的任何信息!这些标签的左边缘应该放在哪里(leading
边缘)?您至少需要为他们三个提供那么多的信息!
我还建议提供右边缘的信息。最简单的方法是将前缘固定到内容 View 的前缘,将后缘固定到内容 View 的后缘。 (然后您可以删除不等式约束。)
最后,您应该删除 item: self.contentView
和 toItem: self
的约束。单元格本身不应该有任何约束——只有它的 contentView
。同样,删除 self.contentView.translatesAutoresizingMaskIntoConstraints = false
行。内容 View 的大小/位置不由您决定,您不应该做任何事情来扰乱它们的确定方式。
关于ios - 由于 AutoLayout,iOS/Swift 中的动态行高折叠为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39185659/