佚名通过本文主要向大家介绍了tablecellcollection,cell collection,自定义cell,ios 自定义cell,matlab定义一个cell等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问题: 自定义的collectionCell无法自适应约束
描述:
解决方案1:
描述:
import UIKit
import SnapKit
class FeatureCollectionViewCell: UICollectionViewCell {
var itemTitleLabel: UILabel!
// MARK: - Initializer & Release
override init(frame: CGRect) {
print("FeatureCollectionViewCell was initialized")
super.init(frame: frame)
setupViews()
setupConstraints()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
print("FeatureCollectionViewCell was released")
}
// MARK: - Extension Method
override func setupViews() {
contentView.backgroundColor = UIColor.grayColor()
itemTitleLabel = UILabel()
itemTitleLabel.translatesAutoresizingMaskIntoConstraints = false
itemTitleLabel.text = "Test Test"
contentView.addSubview(itemTitleLabel)
}
override func setupConstraints() {
itemTitleLabel.snp_makeConstraints { (make) in
make.top.equalTo(contentView).offset(8)
make.centerX.equalTo(contentView)
}
}
// MARK: - Private Method
func configureCell(title: String, image: UIImage) {
itemTitleLabel.text = title
}
override func preferredLayoutAttributesFittingAttributes(layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
let attributes = super.preferredLayoutAttributesFittingAttributes(layoutAttributes)
let newSize = contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
attributes.frame.size = newSize
return attributes
}
}
我是想通过 systemLayoutSizeFittingSize
获取 contentView
的 size
( label的宽高,再加上 label 对于顶部的距离约束),但是却获取的一直是(0, 0)。
PS:没有用 StoryBoard/Xib 。
解决方案1:
你的约束条件不够,必须要有四个要素。