Swift自动布局:子视图中的约束不起作用

时间:2016-10-09 01:19:15

标签: ios swift autolayout

我正在尝试设置一个视图,其中包含两个集合视图和一个中心视图,而视图中又包含元素。

我一直在尝试使用这样的可视格式设置约束:

func setupViews() {

    self.view.addSubview(playGroundView)
    self.view.addSubview(firstCollectionView)
    self.view.addSubview(secondCollectionView)

    self.playGroundView.backgroundColor = UIColor.clear


    aTextView.backgroundColor = UIColor.yellow
    titleTextView.backgroundColor = UIColor.green
    searchBar?.backgroundColor = UIColor.darkGray


    if #available(iOS 9.0, *) {
        playGroundView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
        firstCollectionView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
        secondCollectionView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
    } else {
        // Fallback on earlier versions
    }

    self.playGroundView.addSubview(publishButton)
    self.playGroundView.addSubview(searchBar!)
    self.playGroundView.addSubview(aTextView)
    self.playGroundView.addSubview(titleTextView)

    publishButton.addConstraint(NSLayoutConstraint(item: publishButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 44))
    aTextView.addConstraint(NSLayoutConstraint(item: aTextView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 44))
    titleTextView.addConstraint(NSLayoutConstraint(item: titleTextView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 44))
    searchBar!.addConstraint(NSLayoutConstraint(item: searchBar!, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 44))

    self.playGroundView.addConstraintsWithFormat("V:[v0]-2-|", views: publishButton)
    self.playGroundView.addConstraintsWithFormat("H:[v0]-2-|", views: publishButton)
    self.playGroundView.addConstraintsWithFormat("V:|-2-[v0(44)]", views: searchBar!)


    self.view.bringSubview(toFront: aTextView)

    self.playGroundView.layoutIfNeeded()
    self.playGroundView.layoutSubviews()

    self.view.addConstraintsWithFormat("H:|-8-[v0(100)][v1][v2(100)]-8-|", views: secondCollectionView,playGroundView,firstCollectionView)

    self.view.addConstraintsWithFormat("V:[v0]-2-|", views: playGroundView)
    self.view.addConstraintsWithFormat("V:[v0]-2-|", views: secondCollectionView)
    self.view.addConstraintsWithFormat("V:[v0]-2-|", views: firstCollectionView)



    if(self.isCreate){
        self.titleTextView.text = self.recipeDictionary?.value(forKey: "recipeName") as! String!
    }
}


// Function to set constraints

 func addConstraintsWithFormat(_ format: String, views: UIView...) {

        var viewsDictionary = [String: UIView]()
        for (index,view) in views.enumerated() {
            let key = "v\(index)"
            viewsDictionary[key] = view
            view.translatesAutoresizingMaskIntoConstraints = false
        }
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
    }

我没有看到子视图(playGroundView)中的元素被渲染。有人可以建议我在这里做错了什么吗?

1 个答案:

答案 0 :(得分:3)

我会尝试以下方法进行调试:

  1. 暂时将纯色背景色设置为playGroundView而不是UIColor.clear并运行它以使playGroundView具有正确的大小和位置。
  2. 暂时将边框设置为缺少的子视图并运行它以查看视图是否在playGroundView范围内。或者,您也可以运行代码,转到Xcode>调试>查看调试>捕获视图层次结构
  3. 在添加到playGroundView之前,为publishButtonsearchBar等设置translatesAutoresizingMaskIntoConstraints = false。虽然在帮助函数addConstraintsWithFormat中执行该操作并没有错,但是不需要多次进行该调用。