我正在尝试创建一个具有水平stackView的视图,该视图的第一行有2个按钮,第二行有一个表。这个桌子会增加它的高度,而我的问题是,当桌子的高度太高时,按钮会移到顶部屏幕的外部,而我无法将桌子滚动到底部。如何正确设置约束以使此行为发生?当工作台高度过高时,应仅创建滚动条,而不移动其他组件。你们能帮我吗?
在这里我更新桌子的高度。
func updateTableHeight() {
UIView.animate(withDuration: 0, animations: {
self.tableView.layoutIfNeeded()
}) { (complete) in
self.heightOfTableViewConstraint?.constant = self.tableView.contentSize.height
}
}
我在这里设置所有约束:
private func setUpViewConstraints() {
tableView.translatesAutoresizingMaskIntoConstraints = false
goToNextPageButton.translatesAutoresizingMaskIntoConstraints = false
goToPreviousPageButton.translatesAutoresizingMaskIntoConstraints = false
buttonsStackView.translatesAutoresizingMaskIntoConstraints = false
mainStackView.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .black
let viewsDictionary: [String: Any] = ["tableView": tableView,
"goToNextPageButton": goToNextPageButton,
"goToPreviousPageButton": goToPreviousPageButton,
"mainStackView": mainStackView,
"buttonsStackView": buttonsStackView]
let mainStackViewHorizontalConstraints =
NSLayoutConstraint.constraints(withVisualFormat: "H:|-40-[mainStackView]-40-|",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: nil, views: viewsDictionary)
let mainStackViewVerticalConstraints =
NSLayoutConstraint.constraints(withVisualFormat: "V:[mainStackView]",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: nil, views: viewsDictionary)
let buttonsStackViewVerticalConstraints =
NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[buttonsStackView]",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: nil, views: viewsDictionary)
let buttonsStackViewHorizontalConstraints =
NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[buttonsStackView]-0-|",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: nil, views: viewsDictionary)
let tableViewViewHorizontalConstraints =
NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[tableView]-0-|",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: nil, views: viewsDictionary)
buttonsStackView.addConstraint(NSLayoutConstraint(item: goToPreviousPageButton,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .height,
multiplier: 1,
constant: 80))
buttonsStackView.addConstraint(NSLayoutConstraint(item: goToNextPageButton,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .height,
multiplier: 1,
constant: 80))
view.addConstraint(NSLayoutConstraint(item: mainStackView,
attribute: .top,
relatedBy: .equal,
toItem: view,
attribute: .top,
multiplier: 1,
constant: 40))
view.addConstraints(mainStackViewHorizontalConstraints)
view.addConstraints(mainStackViewVerticalConstraints)
view.addConstraints(buttonsStackViewHorizontalConstraints)
view.addConstraints(buttonsStackViewVerticalConstraints)
mainStackView.addConstraints(tableViewViewHorizontalConstraints)
}