【问题标题】:SWIFT: How to set the frame size of a collection view programmatically so that it's equal to its parent view?SWIFT:如何以编程方式设置集合视图的帧大小,使其等于其父视图?
【发布时间】:2023-03-05 20:17:01
【问题描述】:

我正在以编程方式实现一个 collectionView:

class collectionViews {
     static func collectionViewOne() -> UICollectionView {
        let flowLayout = CarouselFlowLayout()
        let collectionViewOne = UICollectionView(frame: CGRect(x: 106, y: 313, width: 1708, height: 300), collectionViewLayout: flowLayout)
        return collectionViewOne
    }
}

我在 TableView 的 TableViewCell 中显示它,它也是以编程方式实现的:

class TableViewCell2: UITableViewCell {
    var moviesItems: [movieItem] = []
    let cellIdentifier = "movieCardCell"
    let collectionViewOne = collectionViews.collectionViewOne()

    private func setupCollectionView(){

        collectionViewOne.delegate = self
        collectionViewOne.dataSource = self
        collectionViewOne.backgroundColor = UIColor (hex: "444A64")
         collectionViewOne.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
                 let nib = UINib(nibName: "movieCardCell", bundle: nil)
        collectionViewOne.register(nib, forCellWithReuseIdentifier: cellIdentifier)
 self.contentView.addSubview(collectionViewOne)

    }
}

// These functions never get called
 extension TableViewCell:  UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
     print("INSIDE TableViewCell2.collectionView 1")
     return 1
 }
 
 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath)
     print("INSIDE TableViewCell2.collectionView 2")
     return cell
 }
 
 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
     // For some reason he chose the measures of collectionViewCell and substracted 2
     print("INSIDE TableViewCell2.collectionView 3")
     return CGSize(width: 139, height: 64)
 }
 }

我希望collectionView的大小frame等于TableViewCell的frame大小: 那么如果可能的话,知道我应该在这里改变什么吗?

 let collectionViewOne = UICollectionView(frame: CGRect(x: 106, y: 313, width: 1708, height: 300), collectionViewLayout: flowLayout)

【问题讨论】:

    标签: ios swift uitableview uicollectionview tvos


    【解决方案1】:

    您可以在将集合视图约束添加到 setupCollectionView() 方法中的单元格后设置:-

    //#MARK:- Table view cell
     
    class YourTableViewCell: UITableViewCell {
        
        var collectionView: UICollectionView!
        
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            self. setupCollectionView()
        }
        
        required init?(coder: NSCoder) {
            super.init(coder: coder)
        }
        
        func setupCollectionView() {
            
            //        Set collection view
            let layout = UICollectionViewFlowLayout()
            
            collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
            collectionView.backgroundColor = .clear
            collectionView.register(collectionCell.self, forCellWithReuseIdentifier: "collectionCell")
            collectionView.showsVerticalScrollIndicator = false
            collectionView.showsHorizontalScrollIndicator = false
            self.contentView.addSubview(collectionView)
            collectionView.delegate = self
            collectionView.dataSource = self
            
            //Here you can set constraint for collection view
            collectionView.translatesAutoresizingMaskIntoConstraints = false
            collectionView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 0).isActive = true
            collectionView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: 0).isActive = true
            collectionView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 0).isActive = true
            collectionView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 0).isActive = true
            
            layout.scrollDirection = .horizontal
        }
    }
    

    【讨论】:

      【解决方案2】:

      在collectionViews.collectionViewOne()中,改为:

      static func collectionViewOne(frame: CGRect) -> UICollectionView {
              let flowLayout = CarouselFlowLayout()
              let collectionViewOne = UICollectionView(frame: frame, collectionViewLayout: flowLayout)
              return collectionViewOne
          }
      

      在 TableViewCell2 中你必须改变

      let collectionViewOne = collectionViews.collectionViewOne()
      

      let collectionViewOne: UICollectionView!
      

      在 setupCollectionViews() 内部

      collectionViewOne = collectionViews.collectionViewOne(frame: bounds)
      

      你必须把它移到这个函数中,因为一个实例属性不能依赖于它的内联声明中的 self,即你不能在 collectionViewOne 的声明中传递边界。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-29
        相关资源
        最近更新 更多