我正在尝试设置一个视图,其中包含两个集合视图和一个中心视图,而视图中又包含元素。
我一直在尝试使用这样的可视格式设置约束:
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)中的元素被渲染。有人可以建议我在这里做错了什么吗?
答案 0 :(得分:3)
我会尝试以下方法进行调试:
UIColor.clear
并运行它以使playGroundView具有正确的大小和位置。publishButton
,searchBar
等设置translatesAutoresizingMaskIntoConstraints = false。虽然在帮助函数addConstraintsWithFormat
中执行该操作并没有错,但是不需要多次进行该调用。