我想要创建一个受限于超级视图的表格视图,其中包含顶部,左侧和右侧边距,但是要少于或等于底部边距。这样,如果故事视图的内容比其超级视图高,则它将被约束到底部边距并滚动。但如果它是只有3个单元格,并且单元格有自己内部一致的约束条件,表格视图将是例如200点高,他们将在它和超级视图底部边缘之间的空间。到目前为止,在尝试此操作时,如果我没有指定底部约束或将其设置为lessThanOrEqual,我会在可视化调试器中看到一个模糊的布局警告。
答案 0 :(得分:0)
基于UITableViewAutomaticDimension内容的Tableview高度
//make bottom margin constraint @IBOutlet for tableView
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
@IBOutlet weak var tableView: UITableView!
var rowCount = 3
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
将插座连接到tableView底部约束
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
var height: [CGFloat] = [0]
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let indexRowNo = indexPath.row
//will give you cell height for indexPath
let frame = tableView.rectForRow(at: indexPath)
//if cell height already exists in height update value else append
if self.height.count-1 >= indexRowNo{
height.remove(at: indexRowNo)
height.insert(frame.size.height, at: indexRowNo)
}else{
height.append(frame.size.height)
}
if indexPath.row == (tableView.indexPathsForVisibleRows?.last)?.row {
//remove old cell heights if now tableView has less noOfCell after reloadData
if self.height.count >= rowCount{
for _ in rowCount..<self.height.count{
self.height.removeLast()
}
}
}
// total Height for tableView
let totalHeight: CGFloat = height.reduce(0, +)
//e.g. - less/greater ThanOrEqualTo bottom margin conditions
let viewTotalHeight = self.view.frame.height - 80
if totalHeight <= viewTotalHeight{
let value = (self.view.frame.height) - (totalHeight + 10)
self.bottomConstraint.constant = value
self.tableView.isScrollEnabled = false
}else{
self.bottomConstraint.constant = 0
self.tableView.isScrollEnabled = true
}
}