ios - CollectionView 未渲染

标签 ios swift

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

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

空白区域应该是顶部的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/

相关文章:

android - 我可以阻止键盘在显示时向上推吗

Swift 协议(protocol) : property that is subclass of: someClass, 而不是该类

objective-c - CALayer:线性渐变问题

ios - 几周后 Firebase 推送通知不起作用

iOS - 导航栏后退按钮颜色柔和

ios - CTFontManagerUnregisterGraphicsFont 返回 true 但内存未释放

ios - Swift 2.3 到 Swift 3.1 迁移 - Alamofire 请求错误

ios - AVAudioPlayer 无法在共享类中快速工作

ios - UIViewController 中的 UINavigationController 以编程方式在类中

ios - UITableView 在我实现 `estimatedHeightForRowAt indexPath` 后崩溃?