程序员iOS Developer我的Swift开发

Swift-UICollectionView实现照片墙,照片显示

2016-11-29  本文已影响165人  KennyHito
个人链接
微信公众号.jpg
前言:</br>
照片墙
代码分析:

1.创建collectionView

        layout = UICollectionViewFlowLayout.init()//初始化UICollectionViewFlowLayout
        layout.minimumLineSpacing = 10//垂直最小距离
        layout.minimumInteritemSpacing = 10//水平最小距离
        layout.itemSize = CGSize.init(width: 100, height:130)//item的大小
        
        collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: width, height: height), collectionViewLayout: layout)//初始化UICollectionView
        collectionView.backgroundColor = UIColor.white//背景色
        collectionView.showsVerticalScrollIndicator = false//去除垂直滚动条
        collectionView.showsHorizontalScrollIndicator = false//去除水平滚动条
        collectionView.dataSource = self//代理方法
        collectionView.delegate = self
        self.view.addSubview(collectionView)//添加主视图
        collectionView.register(picCollectionViewCell.classForCoder() ,forCellWithReuseIdentifier: "cellID")//注册cell

2.自定制cell

class picCollectionViewCell: UICollectionViewCell {
    var iv:UIImageView?
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createIv()
    }
    
    public func createIv() -> Void {
        iv = UIImageView.init(frame:CGRect.init(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height))
        self.addSubview(iv!)
    }
   
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

3.图片详情页面

    let height = UIScreen.main.bounds.size.height
    let width = UIScreen.main.bounds.size.width
    
    var iv : UIImageView = UIImageView.init()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.navigationItem.title = "详情"
        
        self.showiv()
    }
    
    func showiv() -> Void {
        iv.frame = CGRect.init(x: 0, y: 0, width: width, height: height)
        iv.contentMode = UIViewContentMode.scaleAspectFit
        self.view.addSubview(iv)
    }
时间: 2016.11.29 17:00
声明
上一篇 下一篇

猜你喜欢

热点阅读