以编程方式将约束添加到collectionview

时间:2018-08-11 07:52:27

标签: ios swift uicollectionview constraints

我的Controller UIView和UICollectionView中有两个视图。 我正在迅速而不是情节提要。并且我以这种方式添加约束

// Constraints for First View
FirstView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
FirstView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
FirstView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true

 //Constraints fro Collection View
 collectionView.translatesAutoresizingMaskIntoConstraints = false
 collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50).isActive = true
 collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
 collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
 collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true

但是这会使我的应用程序崩溃。我错了吗? 请帮我怎么做?

2 个答案:

答案 0 :(得分:1)

您忘记了吗?

FirstView.translatesAutoresizingMaskIntoConstraints = false

答案 1 :(得分:0)

我认为出现崩溃是您收藏视图的主要限制 可能会对您有帮助

添加约束:- FirstView.heightAnchor.constraint(equalToConstant:50).isActive = true

更新约束:- collectionView.topAnchor.constraint(等于:FirstView.bottomAnchor,常数:0).isActive = true