Swift之自定义UICollectionViewCell
2017-06-16 21:38
489 查看
自定义UICollectionViewCell和自定义UITableViewCell差不多,不过自定义UICollectionViewCell更像自定义UIView,具体代码如下
import UIKit class ClassifyCollectionViewCell: UICollectionViewCell { var imageView: UIImageView! var titleLabel: UILabel! override init(frame:CGRect){ super.init(frame: frame) setupUI() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func setupUI(){ imageView = UIImageView() titleLabel = UILabel() titleLabel.font = UIFont.systemFont(ofSize: 13) titleLabel.textAlignment = .center titleLabel.textColor = UIColor.black self.addSubview(imageView) self.addSubview(titleLabel) self.backgroundColor = UIColor.white } override func layoutSubviews() { super.layoutSubviews() let frame:CGRect = self.bounds let imgx:CGFloat = 5.0 let imgy = imgx let frameWidth:CGFloat = frame.size.width let imgWidth:CGFloat = frameWidth - (imgx * 2.0) self.imageView.frame = CGRect(x: imgx, y: imgy, width: imgWidth, height: imgWidth) self.titleLabel.frame = CGRect(x: 0, y:imgy+frameWidth , width: frameWidth, height: 20) } override func awakeFromNib() { super.awakeFromNib() // Initialization code } }
相关文章推荐
- swift UICollectionView 代码创建 和自定义Cell
- [IOS]swift自定义uicollectionviewcell
- Swift自定义UICollectionViewCell不显示的解决方案
- 通过Xib创建 UICollectionView 和自定义UICollectionViewCell
- UICollectionViewController xcode6.1 自定义Cell
- iOS开发:xib之UICollectionView自定义cell
- UICollectionView学习<2>之UICollectionViewCell的自定义
- UICollectionView使用实例(第三方类的使用及自定义UICollectionViewCell)
- UICollectionViewCell中cell的定义与自定义方式总结
- 在UITableView或UICollectionView的自定义的cell里添加手势或button,响应方法时如何传参数
- IOS菜鸟的所感所思(四)——自定义UICollectionViewCell以及网络数据的添加。
- iOS UI 18 uicollectionview和自定义cell
- UICollectionView 集合视图用法,自定义Cell
- Swift_ uitableview使用自定义(xib)cell
- 自定义UICollectionViewCell中控件为nil
- 【Swift】iOS UICollectionView 计算 Cell 大小的陷阱
- UI一揽子计划 21 (UICollectionView、SDWebImage第三方类库加载图片的使用、集合视图的布局UICollectionViewFlowLayout 、自定义Cell、布局协议
- iOS UICollectionView 入门 05 创建自定义UICollectionViewCell
- Swift - 使用网格(UICollectionView)的自定义布局实现复杂页面
- IOS swift 自定义Tableview cell