imzyf / ios-swift-learning-notes

📝 iOS Swift Learning Notes - see Issues
MIT License
0 stars 0 forks source link

UICollectionView UICollectionViewCell 相关 #52

Open imzyf opened 6 years ago

imzyf commented 6 years ago

UICollectionViewCell 自适应尺寸

if let fl = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
    fl.estimatedItemSize = CGSize(width: 1, height: 1)
}
imzyf commented 6 years ago

UICollectionViewCell 同时加圆角和阴影

http://qingqinghebiancao.github.io/2016/01/12/iOS%E5%BC%80%E5%8F%91%E5%B0%8F%E6%8A%80%E5%B7%A7/

https://stackoverflow.com/questions/13505379/adding-rounded-corner-and-drop-shadow-to-uicollectionviewcell

self.contentView.layer.cornerRadius = 2.0
self.contentView.layer.borderWidth = 1.0
self.contentView.layer.borderColor = UIColor.clear.cgColor
self.contentView.layer.masksToBounds = true

self.layer.shadowColor = UIColor.lightGray.cgColor
self.layer.shadowOffset = CGSize(width: 0, height: 2.0)
self.layer.shadowRadius = 2.0
self.layer.shadowOpacity = 1.0
self.layer.masksToBounds = false
self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.contentView.layer.cornerRadius).cgPath
imzyf commented 6 years ago

UICollectionView header footer

注册

var nib = UINib(nibName: self.headerName, bundle: nil)
self.collectionView.register(nib, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: self.headerName)

遍历

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

        switch kind {

        case UICollectionElementKindSectionHeader:
            switch indexPath.section {
            case 1:
                let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: self.headerName, for: indexPath)

                return headerView
            case 2:
                let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: self.headerBroderName, for: indexPath)

                return headerView
            default:
                return UICollectionReusableView()
            }
        default:
            return UICollectionReusableView()
        }
    }