I am putting UICollectionView
in custom UITableView
. The UICollectionView
frame set to the contentView
bounds but it's not taking it completely.
CellForRowAtIndexPath
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "collectionViewCell") as! TravelTypeTableViewCell
cell.backgroundColor = .green
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.textLabel?.text = "adsf"
return cell
}
TableViewCell
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let layout = UICollectionViewFlowLayout()
collectionView = UICollectionView(frame: self.contentView.bounds, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
collectionView.backgroundColor = .clear
self.addSubview(collectionView)
collectionView.leftAnchor.constraint(equalTo: self.contentView.leftAnchor, constant:0).isActive = true
collectionView.rightAnchor.constraint(equalTo: self.contentView.rightAnchor, 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
}
ouput UI
You don't need to give frame to collection view if you are using constraints.
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) // no need to give frame as you are adding constraints
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
collectionView.backgroundColor = .clear
self.contentView.addSubview(collectionView)
collectionView.leftAnchor.constraint(equalTo: self.contentView.leftAnchor, constant:0).isActive = true
collectionView.rightAnchor.constraint(equalTo: self.contentView.rightAnchor, 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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.