2014-11-08 70 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