Uicollectionview Cell Subviews Do Not Resize

UICollectionView cell subviews do not resize

The solution was to turn off AutoConstraints for the cell xib and activate the flexible width/height arrows in the AutoResize for the imageviews.

Collection View Dynamic size (DID THE FOLLOWING BUT DIDNOT WORK)

I have also faced same issue so I did another approach. You need to add width constraint and you need to set it constant in systemLayoutSizeFitting function.

The main thing you should be careful about that is add all subviews to contentView of cell and also the last subview must has a constraint to bottom of contentView like the autoResizing TableViewCell

class ABCCell: UICollectionViewCell {

lazy var width: NSLayoutConstraint = {
let width = contentView.widthAnchor.constraint(equalToConstant: bounds.size.width)
width.isActive = true
return width
}()

override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
width.constant = bounds.size.width
return contentView.systemLayoutSizeFitting(CGSize(width: targetSize.width, height: 1))
}
}

Autoresizing issue of UICollectionViewCell contentView's frame in Storyboard prototype cell (Xcode 6, iOS 8 SDK) happens when running on iOS 7 only

contentView is broken. It can be also fixed in awakeFromNib

ObjC:

- (void)awakeFromNib {

[super awakeFromNib];

self.contentView.frame = self.bounds;
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

Swift3:

override func awakeFromNib() {
super.awakeFromNib()

self.contentView.frame = self.bounds
self.contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}

Automatic Label Resize in UICollectionViewCell Not Working

Ok... So thank you to those who have helped. I have found out that my issue was indeed related to Auto-Layout constraints. I simply missed the box that said "Constrain to Margin".

Sample Image

UICollectionViewCell content wrong size on first load

It looks like it still retains it's 100x100px basic size for the contentView while the cell itself gets the right size already. You can force the layout to reset adding the following line just before you return the cell:

cell?.layoutIfNeeded()

Subviews do not resize properly when animating constraints

You need to call layoutIfNeeded() in the animations block, not layoutSubviews().

(You should actually never call layoutSubviews() yourself. It's a method called by the system during a layout pass and only intended for that purpose.)



Related Topics



Leave a reply



Submit