我有以下UIView:
这是一个包含三个子视图的UIView。顶部的常规UILabel(Hello World),自定义UIViewController(CategoryList),其中包含带按钮(alpha,beta,...)的CollectionView和另一个带有标签(SALUT)的自定义UIViewController。
我以编程方式进行自动布局,并将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)
}
这样可行,但由于在SALUT上设置约束后调用viewDidLayoutSubviews(),因此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))
答案 0 :(得分:0)
好的,我发现了这个问题。我错过了将高度约束添加到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
}