所以我有两个不同的 CollectionView,它们在我的 Controller 中呈现,如下所示。
private func configure(){
print("Enter configure function")
func configureViews(){
//takes previously created dynamoCollectionView and assigns it to and creates an instance of DynamoCollectionView
//init for bottom dynamoCollectionView
self.dynamoCollectionView = DynamoCollectionView(frame: .zero)
//init for top dynamoCollectionView
self.dynamoCollectionViewTop = DynamoCollectionViewTop(frame: .zero)
//A Boolean value that determines whether the view’s autoresizing mask is translated into Auto Layout constraints.
self.dynamoCollectionView.translatesAutoresizingMaskIntoConstraints = false
self.dynamoCollectionViewTop.translatesAutoresizingMaskIntoConstraints = false
//will allow you to supply your own data to the bottom collectionView
self.dynamoCollectionView.dataSource = self
//will allow you to send messages about interaction with the bottom dynamoCollectionView to self
self.dynamoCollectionView.delegate = self
//will allow you to supply your own data to the top collectionView
self.dynamoCollectionViewTop.dataSourceTop = self
//will allow you to send messages about interaction with the top dynamoCollectionView to self
self.dynamoCollectionViewTop.delegateTop = self
self.dynamoCollectionView.backgroundColor = .white
self.dynamoCollectionViewTop.backgroundColor = .white
self.view.backgroundColor = .white
self.view.addSubview(self.dynamoCollectionView)
self.view.addSubview(self.dynamoCollectionViewTop)
NSLayoutConstraint.activateViewConstraints(self.dynamoCollectionView, inSuperView: self.view, withLeading: 0.0, trailing: 0.0, top: nil, bottom: nil, width: nil, height: nil)
_ = NSLayoutConstraint.activateVerticalSpacingConstraint(withFirstView: self.topLayoutGuide, secondView: self.dynamoCollectionView, andSeparation: 0.0)
_ = NSLayoutConstraint.activateVerticalSpacingConstraint(withFirstView: self.dynamoCollectionView, secondView: self.bottomLayoutGuide, andSeparation: 0.0)
}
//goes here first
configureViews()
}
第一个 collectionView 是 dynamoCollectionViewTop。我希望它呈现在 dynamoCollectionView 之上,但它似乎根本没有呈现。我尝试模仿约束并进行适当的更改,但它似乎没有渲染。约束如何正确呈现它。
空白区域应该是顶部的collectionView
最佳答案
为dynamoCollectionView添加这些约束
dynamoCollectionView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
dynamoCollectionView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
dynamoCollectionView.bottomAnchor.constraint(equalTo:self.view.bottomAnchor).isActive = true
为 dynamoCollectionViewTop 添加这些约束
dynamoCollectionViewTop.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
dynamoCollectionViewTop.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
dynamoCollectionViewTop.bottomAnchor.constraint(equalTo: dynamoCollectionView.topAnchor).isActive = true
dynamoCollectionViewTop.heightAnchor.constraint(equalToConstant: 300).isActive = true
dynamoCollectionViewTop.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 20).isActive = true
关于ios - CollectionView 未渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48357035/