2014-11-08 47 views
0

設立時自動佈局限制,我想有上具有等距間隔,像這樣(從模擬器)三個標籤的靜態細胞的UITableView一個頁腳視圖:enter image description here奇怪的問題在代碼

我可以使用該委託調用從我的表視圖控制器供應頁腳視圖:

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { 
    // construct view here 
    return view 
} 

而且我可以構建的觀點有兩種方式:

  • 創建標籤,並在C間隔頌歌,並在XIB文件中添加適當的約束
  • 做的一切,然後從文件

我的問題是,第一種方法不起作用,第二次卻加載視圖。

這是我的第一種方法的代碼:

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { 

    if section == 0 { 

     // Create footer view 
     let view = UIView() 
     view.backgroundColor = UIColor.yellowColor() 
     view.clipsToBounds = false 
     view.layer.borderColor = UIColor.greenColor().CGColor 
     view.layer.borderWidth = 2 
     view.setTranslatesAutoresizingMaskIntoConstraints(false) 

     // Create labels 
     var labels: [UIView] = [] 
     for name in ["Label 1", "AAAAAABBB", "Last label"] { 
      let v = UILabel() 

      v.font = UIFont.preferredFontForTextStyle(UIFontTextStyleFootnote) 
      v.textColor = UIColor.darkTextColor() 
      v.textAlignment = .Center 
      v.text = name 
      v.setTranslatesAutoresizingMaskIntoConstraints(false) 

      view.addSubview(v) 
      labels += [v] 
     } 

     // Create spacers 
     var spacers: [UIView] = [] 
     for i in 1...4 { 
      let v = UIView() 

      v.backgroundColor = UIColor.blueColor() // Background color is just so we can see where the view is and what size it has 
      v.setTranslatesAutoresizingMaskIntoConstraints(false) 

      view.addSubview(v) 
      spacers += [v] 
     } 

     // Constrain all views to top and bottom of superview 
     for i in labels + spacers { 
      view.addConstraint(NSLayoutConstraint(item: i, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: 0)) 
      view.addConstraint(NSLayoutConstraint(item: i, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1, constant: 0)) 
     } 

     // Equal width for labels 
     labels.pairs { 
      view.addConstraint(NSLayoutConstraint(item: $0, attribute: .Width, relatedBy: .Equal, toItem: $1, attribute: .Width, multiplier: 1, constant: 0)) 
     } 

     // Equal width for spacers 
     spacers.pairs { 
      view.addConstraint(NSLayoutConstraint(item: $0, attribute: .Width, relatedBy: .Equal, toItem: $1, attribute: .Width, multiplier: 1, constant: 0)) 
     } 

     view.addConstraint(NSLayoutConstraint(item: view, attribute: .Left, relatedBy: .Equal, toItem: spacers[0], attribute: .Left, multiplier: 1, constant: 0)) 
     view.addConstraint(NSLayoutConstraint(item: spacers[0], attribute: .Right, relatedBy: .Equal, toItem: labels[0], attribute: .Left, multiplier: 1, constant: 0)) 
     view.addConstraint(NSLayoutConstraint(item: labels[0], attribute: .Right, relatedBy: .Equal, toItem: spacers[1], attribute: .Left, multiplier: 1, constant: 0)) 
     view.addConstraint(NSLayoutConstraint(item: spacers[1], attribute: .Right, relatedBy: .Equal, toItem: labels[1], attribute: .Left, multiplier: 1, constant: 0)) 
     view.addConstraint(NSLayoutConstraint(item: labels[1], attribute: .Right, relatedBy: .Equal, toItem: spacers[2], attribute: .Left, multiplier: 1, constant: 0)) 
     view.addConstraint(NSLayoutConstraint(item: spacers[2], attribute: .Right, relatedBy: .Equal, toItem: labels[2], attribute: .Left, multiplier: 1, constant: 0)) 
     view.addConstraint(NSLayoutConstraint(item: labels[2], attribute: .Right, relatedBy: .Equal, toItem: spacers[3], attribute: .Left, multiplier: 1, constant: 0)) 
     view.addConstraint(NSLayoutConstraint(item: spacers[3], attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1, constant: 0)) 

     return view 
    } 
    else { 
     return nil 
    } 
} 

extension Array { 
    func pairs(block: (Element, Element?)->()) { 
     if count == 0 { return } 
     if count == 1 { block(self.first!, nil) } 

     var last = self[0] 
     for i in self[1..<count] { 
      block(last, i) 
      last = i 
     } 
    } 
} 

這是結果:

enter image description here

完全不是我所期待的,對不對?

現在,繼續第二種方法。我沒有從Interface Builder發佈大量屏幕截圖,而是專門創建了一個示例項目here來測試此問題。如果打開它,則文件FooterView.xib包含在IB中構建的頁腳視圖,即據我所知具有完全相同的視圖結構和自動佈局約束。

使用該視圖,就像這樣:

return (NSBundle.mainBundle().loadNibNamed("FooterView", owner: self, options: nil).first as UIView) 

得到你的第一張截圖,這正是我想要看到的結果。

因此,使用Interface Builder時,約束會按預期工作。 爲什麼在代碼中創建視圖&約束時它不工作?我錯過了什麼?

回答

2

視圖的大小是在創建時未知,所以設置了setTranslatesAutoresizingMaskIntoConstraints到真正的伎倆:

view.setTranslatesAutoresizingMaskIntoConstraints(true) 

結果:

enter image description here

+0

它的工作原理!但爲什麼?有什麼'setTranslatesAutoresizingMaskIntoConstraints'用於創建時未知的大小? – Alex 2014-11-08 15:31:48

+0

我注意到視圖沒有得到'tableView.frame.width'的寬度,並試圖手動設置它。不幸的是,這不起作用,所以我玩了'setTranslatesAutoresizingMaskIntoConstraints'。我認爲這個視圖現在獲得了會自動將其重新設置爲tableView寬度的contstaints。 – zisoft 2014-11-08 16:35:43

+0

我現在看到了。謝謝! – Alex 2014-11-08 16:47:07