简体   繁体   English

SWIFT:如何以编程方式设置集合视图的框架大小,使其等于其父视图?

[英]SWIFT: How to set the frame size of a collection view programmatically so that it's equal to its parent view?

I am implementing a collectionView programmatically:我正在以编程方式实现 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
    }
}

I'm displaying it inside a TableViewCell of TableView that's also implemented programatically:我将它显示在 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)
 }
 }

I want the size frame of the collectionView to be equal to the size of the frame of the TableViewCell : So any idea what should I change here if this is possible?我希望 collectionView 的大小框架等于TableViewCell框架的大小:如果可能的话,我应该在这里更改什么?

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

You can set collection view constraints after adding it to a cell inside your setupCollectionView() method:-您可以在将集合视图添加到 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
    }
}

In collectionViews.collectionViewOne(), change to:在 collectionViews.collectionViewOne() 中,更改为:

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

and in TableViewCell2 you'll have to change在 TableViewCell2 中,您必须更改

let collectionViewOne = collectionViews.collectionViewOne()

to

let collectionViewOne: UICollectionView!

and inside of setupCollectionViews()和 setupCollectionViews() 内部

collectionViewOne = collectionViews.collectionViewOne(frame: bounds)

You have to move it into this function because an instance property cannot rely on self in it's inline declaration, ie you can't pass bounds in the declaration of collectionViewOne.你必须把它移到这个函数中,因为一个实例属性在它的内联声明中不能依赖于 self,即你不能在 collectionViewOne 的声明中传递边界。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式为其父视图添加视图大小? - How to make a programmatically added view size to its parent view? 如何在自定义视图中将子视图的框架设置为其父框架 - How to set a subview's frame to be its parent's frame in a custom view 如何设置集合视图单元格大小与iOS中的集合视图完全相同? - How to set the collection view cell size exactly equal to the collection view in iOS? 如何将视图高度设置为等于其宽度 - How to set a view height to be equal to its width 如何在Objective C中以编程方式设置集合视图? - How to set collection view programmatically in Objective C? 如何快速动态更改集合视图的大小? - How to dynamically change size of collection view in swift? 相对于屏幕尺寸快速设置收藏夹视图单元格的尺寸 - Set collection view cell size relative to screen size swift 如何在 Swift 中设置自定义视图的内在内容大小? - How to set a custom view's intrinsic content size in Swift? 故事板视图(iphone 4/5/6)布局影响集合单元大小(以编程方式设置) - Storyboard view(iphone 4/5/6) layout effecting collection cell size (programmatically set) 设置变量等于视图的框架,而视图的框架更改时该值不变 - Set a variable equal to a view's frame without the value changing when view's frame changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM