コレクションビューを使用して、プロファイルイメージのコレクションとその人物の名前を表示しています。だから私のセルは、サブビューとしてUIImageViewとUILabelを持っています。利用可能なスペースに応じてセルのサイズを計算するUICollectionView collectionView:cellForItemAtIndexPathが完了した後、セルのサブビューのサイズが変更されます
collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
:私はUICollectionViewDelegateFlowLayoutメソッドを使用しています。
これはすべて正常に動作しています。セルのサブビューに制約を追加し、それに応じてサイズを変更しました。
私が実行している問題は、UIImageViewsをサークルにしたいということです。自動レイアウトは、その効果を適用するまで、セルのサブビューサイズを再計算しないようです。代わりに、私がimageViewのcornerRadiusを計算すると、画像の幅が114.0(ストーリーボードにあるもの)であることがまだ分かります。この結果、iPhone 5では円が表示されますが、大きなデバイスでは角が丸くなります。ここではそのための私のコードは次のとおりです。
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PersonCell", forIndexPath: indexPath)
configureCell(cell, atIndexPath: indexPath)
return cell
}
func configureCell(cell: UICollectionViewCell, atIndexPath indexPath: NSIndexPath) {
if let person = personAtIndexPath(indexPath) {
let imageView = cell.viewWithTag(100) as! UIImageView
let nameLabel = cell.viewWithTag(200) as! UILabel
cell.contentView.frame = cell.bounds
cell.contentView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
circularizeImageView(imageView)
imageView.image = person.profileImage
nameLabel.text = person.name
}
}
func circularizeImageView(imageView: UIImageView) {
imageView.layer.cornerRadius = (CGRectGetWidth(imageView.bounds)/2)
imageView.clipsToBounds = true
imageView.layer.borderWidth = 2.0
imageView.layer.borderColor = UIColor.whiteColor().CGColor
}
私は以前にサブビューがここのように、すべてのサイズを変更していないでしょうカップルの場所で見た:UICollectionView cell subviews do not resize
私は、これはもはや問題ではないと思います、しかし、私はあなたのconfigureCell()で見ることができるように私のコードに修正を追加しましたが、それはまだ助けていません。
したがって、cellForItemAtIndexPath呼び出しが完了するまで、これらのサブビューのサイズが変更されないようです。どのように私はこれに対処するかもしれないかについての任意の考えですか?スクリーンショット:rounded corners on UIImageViews instead of complete circles
ありがとう@ bsmith11。それが私の問題でした。私はUICollectionViewCellをサブクラス化していませんでした(これがviewWithTag()を使用していた理由です)。今私は、セルをサブクラス化し、そのサブクラスのlayoutSubviews()のcicularizeImageView()を呼び出します。それは完全に動作し、私のviewControllerはもはやセルのレイアウトの責任を負いませんので、私のコードははるかにクリーンです。 – Jadsada