ダイナミックなUICollectionViewを作成しようとしていますが、その内部のテキストに基づいてセルのサイズが自動的に変更されます。しかし、いくつかの理由で、私のカスタムUICollectionViewCellは、全幅に拡張されません。私はSnapKitをAutoLayoutとして使用しています。私のすべてのビューはコードベースです。 xibやストーリーボードはありません。ここで私は、現時点で得たもののデバッグビューです:AutoLayoutがiOS 10で機能しないUICollectionViewCell
私は、セルがいっぱいの幅と内容が何であれ合わせて高さを拡張します。ここで私はちょうど..上とで、この上で感謝何かアドバイスを働いて3日間を過ごしてきた私のUICollectionViewController
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Home"
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: view.frame.width, height: view.frame.height)
layout.scrollDirection = .vertical
layout.estimatedItemSize = CGSize(width: 375, height: 250)
collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height), collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.backgroundColor = UIColor(red: 245/255, green: 245/255, blue: 245/255, alpha: 1)
collectionView.register(HomeCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
self.view.addSubview(collectionView)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
// Configure the cell
if let c = cell as? HomeCollectionViewCell {
c.contentView.frame = c.bounds
c.contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
configureCell(c, indexPath: indexPath)
}
return cell
}
func configureCell(_ cell: HomeCollectionViewCell, indexPath: IndexPath) {
cell.setText(withTitle: items[(indexPath as NSIndexPath).section].title)
}
そして、ここで私のカスタムのスニペットがUICollectionViewCellだ
override init(frame: CGRect) {
super.init(frame: frame)
self.contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
self.contentView.translatesAutoresizingMaskIntoConstraints = true
self.contentView.bounds = CGRect(x: 0, y: 0, width: 99999, height: 99999)
createTitle()
}
private func createTitle() {
titleView = TTTAttributedLabel(frame: CGRect(x: 0, y: 0, width: contentView.frame.width, height: 56))
titleView.tag = 1
titleView.numberOfLines = 2
titleView.delegate = self
titleView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(titleView)
titleView.snp_updateConstraints(closure: {
make in
make.leading.trailing.equalTo(contentView).offset(10)
make.top.equalTo(contentView).offset(10)
make.bottom.equalTo(contentView).offset(-10)
})
}
func setText(withTitle title:String, paragraph: String, image: String) {
let titleAttributed = AttributedString(string: title, attributes: titleStringAttr)
titleView.attributedText = titleAttributed
titleView.sizeToFit()
}
の抜粋です!
のためにそこにrobertjpayneするクレジットあなたが実装しようとしたことがあり、 " - (CGSize)collectionView:(UICollectionView *)collectionViewレイアウト:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPathを"? – Tom
hmm私はサイズを動的にしたいと思ったら、それを実装する必要はありませんか?私は前にそれを試して、 'layout.estimatedItemSize'を削除して、すべて同じサイズになりました。 – yonasstephen
あなたはこの問題の答えに到達しましたか? UITableViewCellで非常に似たような問題が発生しました。 – JohnZ