gpt4 book ai didi

ios - UIViewController : adjust contraints of xib

转载 作者:行者123 更新时间:2023-11-28 13:13:30 24 4
gpt4 key购买 nike

在我的项目中,我定义了一个 xib 和一个将包含 xib 的 ViewController。

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 边框没有“粘”到 Controller 的主视图。

enter image description here

边框图例:

  • 红色:self.view
  • 黄色:feedDetail
  • 其他:feedDetail 中的元素(仅测试)

最佳答案

尝试在添加约束之前调用此方法。

feedDetail.setTranslatesAutoresizingMaskIntoConstraints(false)

关于ios - UIViewController : adjust contraints of xib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30261874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com