我以编程方式设计了一个弹出视图,其中包含标题,tableView和2个按钮。按钮水平放置在一排。我努力了,但我不知道怎么办不能连续设置按钮(即水平)。
同样,tableView应根据单元格的数量设置其约束。它不应该滚动。任何帮助或建议非常感谢。 这是我的代码:
class PopUpMenu : UIView{
// The title label
lazy var titleLabel: UILabel = {
let label = UILabel(frame: .zero)
label.translatesAutoresizingMaskIntoConstraints = false
label.textAlignment = .center
return label
}()
// The table view
lazy var tableView: UITableView = {
let tv = UITableView(frame: .zero)
tv.translatesAutoresizingMaskIntoConstraints = false
return tv
}()
// add buttons
lazy var previousButton : UIButton = {
let y = self.tableView.frame.origin.y+self.tableView.frame.height
let frame = CGRect(x: 10, y: y, width: 150, height: 36)
let button = UIButton()
button.frame = frame
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Previous", for: .normal)
return button
}()
lazy var nextButton : UIButton = {
let x = previousButton.frame.origin.x + previousButton.frame.width + 10
let y = self.tableView.frame.origin.y+self.tableView.frame.height
let frame = CGRect(x: x, y: y, width: previousButton, height: 36)
let button = UIButton()
button.frame = frame
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Next", for: .normal)
return button
}()
override func didMoveToSuperview() {
super.didMoveToSuperview()
// Add views
addSubview(titleLabel)
addSubview(tableView)
addSubview(previousButton)
addSubview(nextButton)
var constraints = [NSLayoutConstraint]()
let views: [String: UIView] = ["titleLabel": titleLabel, "tableView": tableView, "previousButton" : previousButton, "nextButton" : nextButton]
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|[titleLabel]|", options: [], metrics: nil, views: views)
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|[tableView]|", options: [], metrics: nil, views: views)
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-[previousButton]-[nextButton]-|", options: [], metrics: nil, views: views)
constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|[titleLabel(50)][tableView][previousButton][nextButton]|", options: [], metrics: nil, views: views)
NSLayoutConstraint.activate(constraints)
}
}
这是我的输出:
答案 0 :(得分:0)
这帮助我解决了这个问题。 https://gist.github.com/gazolla/19c7771b33fa5f781bc7f5b0aa842dbd
我将view
替换为button
,并将stackView
作为子视图添加到我的自定义视图中并设置约束。