2
我想添加programmaticaly幾個constarints,當我從底部的新表視圖滑動,但到目前爲止,我不明白我做錯了什麼。在我的super view
我有map view
必須更改bottom constaint
爲單擊按鈕時新的tableView
留出空間。添加約束programmaticaly使崩潰
Bacically我的表視圖,我需要4個constaints:
- 頂部constaint到地圖底部
- 底部constarint標籤欄頂部constarint
- 領導和trailinf從超級視圖
所有時間我碰到:
Impossible to set up layout with view hierarchy unprepared for constraint.
基於從設定限制:here
下面是一個代碼:
@IBAction func test(_ sender: UIButton) {
let tableView = UITableView()
tableView.backgroundColor = UIColor.red
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tableView)
let topConstraint = NSLayoutConstraint(item: tableView, attribute: .top, relatedBy: .equal, toItem: self.mapView, attribute: .bottom, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: tableView, attribute: .bottom, relatedBy: .equal, toItem: self.tabBarController?.tabBar, attribute: .top, multiplier: 1, constant: 0)
let leadingConstraint = NSLayoutConstraint(item: tableView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: tableView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: 0)
view.addConstraints([topConstraint, bottomConstraint, leadingConstraint, trailingConstraint])
// tableView.addConstraint(NSLayoutConstraint(item: tableView, attribute: .top, relatedBy: .equal, toItem: self.mapView, attribute: .bottom, multiplier: 1, constant: 0))
// tableView.addConstraint(NSLayoutConstraint(item: tableView, attribute: .bottom, relatedBy: .equal, toItem: self.tabBarController?.tabBar, attribute: .top, multiplier: 1, constant: 0))
// tableView.addConstraint(NSLayoutConstraint(item: tableView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 0))
// tableView.addConstraint(NSLayoutConstraint(item: tableView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: 0))
self.view.layoutIfNeeded()
UIView.animate(withDuration: 0.5, animations: {
self.mapBottomConstaint.constant = 200
self.centerButtonBottomConstaint.constant = 208
self.view.layoutIfNeeded()
})
}
提前感謝!
你爲什麼要提供表格視圖?我認爲這不是必需的。 –
@PremaJanoti你說得對,他們不是! – yerpy
現在工作嗎? –