Swift Uicollectionview单元格仅环绕文本

Swift Uicollectionview单元格仅环绕文本,swift,uicollectionview,uicollectionviewcell,nslayoutconstraint,Swift,Uicollectionview,Uicollectionviewcell,Nslayoutconstraint,我的swift代码集合ViewCell位于所有代码中,而不是故事板中。正如你在我下面的照片中看到的。uicollectionviewcell环绕一个数字,我希望collectionviewcell中有更多的空间。我不知道如何使用下面的代码向uicollectionviewcell添加Hight约束。简言之,只需将每个收集单元设置为100高100宽,谢谢 导入UIKit 类单元格:UICollectionViewCell{ 静态变量标识符:String=“Cell” 弱var textLabel

我的swift代码集合ViewCell位于所有代码中,而不是故事板中。正如你在我下面的照片中看到的。uicollectionviewcell环绕一个数字,我希望collectionviewcell中有更多的空间。我不知道如何使用下面的代码向uicollectionviewcell添加Hight约束。简言之,只需将每个收集单元设置为100高100宽,谢谢

导入UIKit
类单元格:UICollectionViewCell{
静态变量标识符:String=“Cell”
弱var textLabel:UILabel!
重写初始化(帧:CGRect){
super.init(frame:frame)
设textLabel=UILabel(帧:.0)
textLabel.translatesAutoResizezingMaskintoConstraints=false
self.contentView.addSubview(textLabel)
NSLayoutConstraint.activate([
self.contentView.centerXAnchor.constraint(equalTo:textlab.centerXAnchor),
自高度锚定约束(等恒量:100),
self.contentView.centerYAnchor.constraint(等式:textlab.centerYAnchor),
])
self.textlab=textlab
self.reset()
textLabel.backgroundColor=.yellow
}
必需的初始化?(编码器aDecoder:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
重写func prepareForReuse()函数{
super.prepareforeuse()
self.reset()
}
函数重置(){
self.textLabel.textAlignment=.center
}
}
类ViewController:UIViewController{
弱var collectionView:UICollectionView!
变量数据:[Int]=数组(0..Int{
返回self.data.count
}
func collectionView(collectionView:UICollectionView,
cellForItemAt indexPath:indexPath)->UICollectionViewCell{
让cell=collectionView.dequeueReusableCell(withReuseIdentifier:cell.identifier,for:indexPath)作为!cell
让data=self.data[indexPath.item]
cell.textlab.text=字符串(数据)
返回单元
}
}
扩展视图控制器:UICollectionViewDelegate{
func collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath){
}
}
扩展视图控制器:UICollectionViewDelegateFlowLayout{
func collectionView(collectionView:UICollectionView,
布局集合视图布局:UICollectionViewLayout,
sizeForItemAt indexPath:indexPath)->CGSize{
返回CGSize(宽度:collectionView.bounds.width,高度:60)
}
func collectionView(collectionView:UICollectionView,
布局集合视图布局:UICollectionViewLayout,
插入部分:Int)->UIEdgeInsets{
返回UIEDGEINSET(顶部:0,左侧:0,底部:0,右侧:0)/.0
}
func collectionView(collectionView:UICollectionView,
布局集合视图布局:UICollectionViewLayout,
截面上的最小最小材料间距:Int)->CGFloat{
返回0
}
func collectionView(collectionView:UICollectionView,
布局集合视图布局:UICollectionViewLayout,
截面的最小线间距:Int)->CGFloat{
返回0
}
}

更新sizeForItemAt方法以返回w=100 h=100

extension ViewController: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 100, height: 100)
    }
}

更新sizeForItemAt方法以返回w=100 h=100

extension ViewController: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 100, height: 100)
    }
}

look through UICollectionViewDelegateFlowLayout它有函数调整单元格大小look through UICollectionViewDelegateFlowLayout它有函数调整单元格大小若要完全填充collectionView,请返回CGSize(宽度:collectionView.frame.size.width,高度:100)若要完全填充collectionView,请返回CGSize(宽度:collectionView.frame.size.width,高度:100)