[英]SWIFT: How to set the frame size of a collection view programmatically so that it's equal to its parent view?
我正在以編程方式實現 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 的大小框架等於TableViewCell
框架的大小:如果可能的話,我應該在這里更改什么?
let collectionViewOne = UICollectionView(frame: CGRect(x: 106, y: 313, width: 1708, height: 300), collectionViewLayout: flowLayout)
您可以在將集合視圖添加到 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
}
}
在 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 的聲明中傳遞邊界。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.