2017-04-27 62 views
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() 
     }) 
    } 

提前感謝!

+1

你爲什麼要提供表格視圖?我認爲這不是必需的。 –

+0

@PremaJanoti你說得對,他們不是! – yerpy

+0

現在工作嗎? –

回答

2

您正在嘗試爲視圖層次結構之外的視圖設置約束。自動佈局系統提供佈局指南,幫助您將視圖與導航欄和選項卡欄對齊。

在底部約束條件中,將self.tabBarController?.tabBar替換爲bottomLayoutGuide

作爲一個側面說明,新的佈局語法(用於iOS 9+)使得更加容易,而不第三方庫創建程序的限制:

let topConstraint = tableView.topAnchor.constraint(equalTo: mapView.bottomAnchor) 
let bottomConstraint = tableView.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor) 
let leadingConstraint = tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor) 
let trailingConstraint = tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor) 

更多地用於創建方案限制here信息。