2017-05-31 72 views
0

我有以下的UIView:如何使用动态高度在子视图下方定位子视图?

enter image description here

它包含三个子视图一个UIView。顶部是一个常规的UILabel(Hello World),一个包含带有按钮(alpha,beta,...)的CollectionView的自定义UIViewController(CategoryList)和另一个带有标签(SALUT)的自定义UIViewController。

我做

sCtrl.view.topAnchor.constraint(equalTo: catList.view.bottomAnchor).isActive = true 

这导致图像的上方,其中SALUT不位于类别列表如下面我自动布局编程和所属分类(VAR catList)以下位置SALUT(VAR SCTRL)会喜欢它。我有点理解为什么,因为当我设置约束时,按钮还没有正确放置在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)) 
+0

你可以检查你的'CategoryList'类是否有内在的内容大小? –

回答

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 
} 
相关问题