CollectionView不呈现

时间:2018-01-20 14:02:35

标签: ios swift

所以我有两个不同的CollectionViews,它们在我的控制器中渲染,就像这样。

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上面呈现,但似乎根本没有呈现。我尝试模仿约束并进行适当的更改,但它似乎没有呈现。约束如何正确呈现它。

我的屏幕目前看起来像这样 enter image description here

空白区域应该是顶部的collectionView

1 个答案:

答案 0 :(得分:0)

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