2015-05-15 82 views
0

在我的項目中,我定義了一個xib和一個包含xib的ViewController。UIViewController:調整xib的限制

class FeedDetailViewController: UIViewController, FeedDetailDelegate { 

    var feedDetail: FeedDetail! 

    override func viewDidLoad() { 
    super.viewDidLoad() 
    feedDetail = (NSBundle.mainBundle().loadNibNamed("FeedDetail", owner: self, options: nil)[0] as? UIView)! as! FeedDetail 
    feedDetail.delegate = self; 
    self.view.addSubview(feedDetail) 
    self.adjustContraints() 
    } 

func adjustContraints() { 

    feedDetail.sizeThatFits(self.view.frame.size) 

    var constraint = NSLayoutConstraint(item: feedDetail, 
     attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0) 
    view.addConstraint(constraint) 

    constraint = NSLayoutConstraint(item: feedDetail, 
     attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0) 
    view.addConstraint(constraint) 

    constraint = NSLayoutConstraint(item: feedDetail, 
     attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0) 
    view.addConstraint(constraint) 

    constraint = NSLayoutConstraint(item: feedDetail, 
     attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0) 
    view.addConstraint(constraint) 

    self.view.layoutIfNeeded() 

} 

我想要的只是xib在「全屏」,但xib邊框並沒有「貼」到控制器的主視圖。

enter image description here

邊境傳奇:

  • 紅:self.view
  • 黃:feedDetail
  • 人:內feedDetail元素(僅測試)

回答

2

試圖調用此方法在添加約束之前。

feedDetail.setTranslatesAutoresizingMaskIntoConstraints(false)