我有以下UIView:

它是一个包含三个子视图的UIView。顶部的常规UILabel (Hello World)、自定义UIViewController (CategoryList),其中包含带有按钮(alpha、beta等)的CollectionView和另一个只有一个标签的自定义UIViewController (SALUT)。
我以编程方式进行自动布局,并使用以下命令将SALUT (var sCtrl)放置在CategoryList (var catList)之下
sCtrl.view.topAnchor.constraint(equalTo: catList.view.bottomAnchor).isActive = true这导致了上面的图片,其中SALUT并不像我希望的那样位于类别列表的下方。我在某种程度上理解了为什么,因为当我设置约束时,按钮还没有在CollectionView中正确布局,因此catList的底部锚点也没有设置。
在CategoryList:UIVIewController中,为了获得集合视图以获得正确的高度,我拥有以下内容。
override public func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
heightConstraint = collectionView.heightAnchor.constraint(equalToConstant: collectionView.collectionViewLayout.collectionViewContentSize.height)
self.view.addConstraint(heightConstraint)
}这是可行的,但由于viewDidLayoutSubviews()是在对SALUT设置约束之后调用的,因此SALUT位置是错误的。我怎样才能以一种简单的方式把它做好呢?(我想我可以为我的主视图做一个控制器,并在子视图布局时检查那里,然后更新子视图的位置,但这对我来说似乎太复杂了)。
下面是主视图的完整代码,因此您可以看到布局定位代码。如果需要,我也可以提供子视图代码,但我认为不应该需要它,因为它们应该被视为黑盒……
class MyView: UIView {
let label = UILabel()
var sCtrl : SubController
var catList : CategoryList
var topConstraint = NSLayoutConstraint()
override init(frame: CGRect) {
sCtrl = SubController()
catList = CategoryList()
super.init(frame: frame)
setup()
}
private func setup() {
self.backgroundColor = UIColor.green
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = UIColor.yellow
label.text = "Hello world"
label.textAlignment = .center
self.addSubview(label)
catList.view.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(catList.view)
sCtrl.view.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(sCtrl.view)
let margins = self.layoutMarginsGuide
label.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: 0).isActive = true
label.trailingAnchor.constraint(equalTo: margins.trailingAnchor, constant: 0).isActive = true
label.topAnchor.constraint(equalTo: margins.topAnchor, constant: 0).isActive = true
label.heightAnchor.constraint(equalToConstant: 100.0).isActive=true
catList.view.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: 0).isActive = true
catList.view.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 1.0).isActive = true
catList.view.widthAnchor.constraint(equalTo: margins.widthAnchor, multiplier: 1.0).isActive = true
catList.view.trailingAnchor.constraint(equalTo: margins.trailingAnchor, constant:0).isActive = true
sCtrl.view.topAnchor.constraint(equalTo: catList.view.bottomAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
let v = MyView(frame: CGRect(x: 0, y: 0, width: 400, height: 600))发布于 2017-05-31 19:23:12
好了,我找到问题了。我没有将高度约束添加到CategoryList自己的视图中。加上这一点,它工作得很好。
override public func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
heightConstraint = collectionView.heightAnchor.constraint(equalToConstant: collectionView.collectionViewLayout.collectionViewContentSize.height)
self.view.addConstraint(heightConstraint)
//THE MISSING LINE!!
self.view.heightAnchor.constraint(equalTo: self.collectionView.heightAnchor, constant: 0).isActive = true
} https://stackoverflow.com/questions/44281474
复制相似问题