我在UIScrollView中有一个表和一个按钮,我想要以下设计:
在以下限制条件下,我设法使按钮始终始终在表格的最后一行下方40 pts,并且在表格足够长的情况下在视图末端上方83 pt。在我看来,bottomConstraint
的优先级没有适当地覆盖topConstraint
的约束。我已将滚动视图设置为涵盖整个屏幕。
/* - Sign Out button is 40 pts tall - */
let heightConstraint = NSLayoutConstraint(item: signOutBtn, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 41)
/* - Sign Out button is ALWAYS 83 pts above bottom of screen, when visible - */
let bottomConstraint = NSLayoutConstraint(item: signOutBtn, attribute: .bottom, relatedBy: .equal, toItem: scrollView, attribute: .bottom, multiplier: 1, constant: -83)
bottomConstraint.priority = UILayoutPriority.required
/* - Sign Out button is AT LEAST 40 pts below last row of table - */
let topConstraint = NSLayoutConstraint(item: signOutBtn, attribute: .top, relatedBy: .greaterThanOrEqual, toItem: tableView, attribute: .bottom, multiplier: 1, constant: 40)
topConstraint.priority = UILayoutPriority.defaultLow
/* - Sign Out button stretches across the screen - */
let leadingConstraint = NSLayoutConstraint(item: signOutBtn, attribute: .leading, relatedBy: .equal, toItem: scrollView, attribute: .leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: signOutBtn, attribute: .trailing, relatedBy: .equal, toItem: scrollView, attribute: .trailing, multiplier: 1, constant: 0)
scrollView.addConstraints([heightConstraint, bottomConstraint, leadingConstraint, trailingConstraint, topConstraint])
如果表格过长,则不会显示“退出”按钮,用户需要向下滚动至该按钮。
答案 0 :(得分:0)
我创建了以下视图控制器,该控制器应该可以解决您的问题-我必须承认,我不知道为什么在布局中使用了scrollView,但是我希望我能够重新创建您的设置。
为了模拟不同的桌子高度,我使用了tableView.heightAnchor.constraint(equalToConstant: 300)
。
Anchors用于创建约束。
import UIKit
class ViewController: UIViewController {
let scrollView = UIScrollView()
let tableView = UITableView()
let signOutBtn = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(scrollView)
scrollView.addSubview(tableView)
scrollView.addSubview(signOutBtn)
tableView.dataSource = self
signOutBtn.setTitle("BUTTON", for: .normal)
signOutBtn.setTitleColor(.blue, for: .normal)
//ScrollView constraints
scrollView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor)
])
//TableView constraints
tableView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: scrollView.safeAreaLayoutGuide.topAnchor),
tableView.bottomAnchor.constraint(lessThanOrEqualTo: signOutBtn.topAnchor, constant: -40),
tableView.heightAnchor.constraint(equalToConstant: 300),
tableView.trailingAnchor.constraint(equalTo: scrollView.safeAreaLayoutGuide.trailingAnchor),
tableView.leadingAnchor.constraint(equalTo: scrollView.safeAreaLayoutGuide.leadingAnchor)
])
//SignOutButton constraints
signOutBtn.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
signOutBtn.heightAnchor.constraint(equalToConstant: 41),
signOutBtn.bottomAnchor.constraint(equalTo: scrollView.safeAreaLayoutGuide.bottomAnchor, constant: -83),
signOutBtn.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 30
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "")
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
}
我还将屏幕附加结果:
答案 1 :(得分:0)