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

I did what this answer said but it did not work why?
How to adjust height of UICollectionView to be the height of the content size of the UICollectionView?
but there is a problem its not resizing what am i missing out

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))
}
}

Related

Expand/Collapse UITableView always gets the wrong height

As this video, (you can download source code from here, I've tried my best to remove all the unnecessary code)
I need to manually scroll through tableview to make it reset to the right height of each cell and if I expand and unexpand multiple cells, it will show the wrong height again.
I'm trying to use "systemLayoutSizeFittingSize" instead of "heightForRowAt". (CustomCell.swift)
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
setNeedsLayout()
layoutIfNeeded()
let defaultHeight: CGFloat = 90
return CGSize(width: 0, height: !isExpanded ? defaultHeight : tableView.contentSize.height + defaultHeight)
}
I want to use autolayout instead of giving the exact height for tableview. what do I do wrong?

how can i stretch collectionView height in tableViewCell?

i want to stretch collectionView height in tableView.
as the gitHub App's Label (below picture),
when the collectionViewCell's width excess collectionView's width,
I want to set collectionViewCell in a new line
To set cell in the new line, i tried to use flowlayout.
but it did not be increased height. just can scroll in collectionView. 😭.
unfortunately, tableView's height did not be increased
what's the best way to do this ?😂
This approach from SRP-Achiever should work.
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
collectionView.layoutIfNeeded()
collectionView.frame = CGRect(x: 0, y: 0, width: targetSize.width , height: 1)
return collectionView.collectionViewLayout.collectionViewContentSize
}

UICollectionView Scroll Jumpy Initially

I am using a UICollectionView for chat and using scroll to bottom on load. It's all working good except the first time I scroll manually it bounces up about 50-100 pt. If I continue scrolling it's fine and goes into the safe area and below the screen.
What is causing the initial bounce? It seems that it bounces off the top of the bottom safe area first, and then goes below the safe area fine if you continue scrolling.
What I'm trying to get is a nice smooth scroll initially without the weird bounce.
It seems to be caused by the fact that I am dynamically sizing image heights after they load with invalidateLayout.
I was able to solve this by luck with the following
AutoSizing cells: cell width equal to the CollectionView
// UICollectionViewController
if let layout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
layout.estimatedItemSize = CGSize(width: view.frame.width, height: 100)
}
// UICollectionViewCell
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
let autoLayoutAttributes = super.preferredLayoutAttributesFitting(layoutAttributes)
// Specify you want _full width_
let targetSize = CGSize(width: layoutAttributes.frame.width, height: 200)
// Calculate the size (height) using Auto Layout
let autoLayoutSize = contentView.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: UILayoutPriority.required, verticalFittingPriority: UILayoutPriority.defaultLow)
let autoLayoutFrame = CGRect(origin: autoLayoutAttributes.frame.origin, size: autoLayoutSize)
// Assign the new size to the layout attributes
autoLayoutAttributes.frame = autoLayoutFrame
return autoLayoutAttributes
}

Is it possible to change the height of collection view with its dynamic cell height? which delegate method should we use if possible?

I have to change the height of collection view with its dynamic cell height. cell height depends on its content and that cell height should update the height of collection view.
If you're using UICollectionViewFlowLayout then you can use self sizing cells for UICollectionView and UITableView feature available from iOS 8 and above. This feature helps in dynamically setting the height of the cell based on the content and constraints set in the cell.
There are 2 step setup you need to do for self sizing cells to work.
1. Set estimatedItemSize on UICollectionViewFlowLayout:
//for storyboard:
if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = CGSize(width: UIScreen.main.bounds.width, height: 100)
}
//using code:
let flowLayout = UICollectionViewFlowLayout()
let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowLayout)
flowLayout.estimatedItemSize = CGSize.init(width: UIScreen.main.bounds.width, height: 100)
2. Use Autolayout to configure custom UICollectionView cell or implement
preferredLayoutAttributesFittingAttributes in your custom cell
var isHeightCalculated: Bool = false
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
//Exhibit A - We need to cache our calculation to prevent a crash.
if !isHeightCalculated {
setNeedsLayout()
layoutIfNeeded()
let size = contentView.systemLayoutSizeFitting(layoutAttributes.size)
var newFrame = layoutAttributes.frame
newFrame.size.width = CGFloat(ceilf(Float(size.width)))
layoutAttributes.frame = newFrame
isHeightCalculated = true
}
return layoutAttributes
}
read stackoverflow and blog for more info on how to use autolayout for self sizing cells.

self-sizing uitableviewcell not recalculate content size when subview instrinsic content size has changed

everyone, i have issue about uitableviewcell
custom uitableview cell which has subviews, one of which is custom image view named CustomIntrinsicImageView, and code as below:
class CustomIntrinsicImageView: UIImageView {
var _preferredMaxLayoutWidth: CGFloat!
var preferredMaxLayoutWidth: CGFloat! {
get {
return _preferredMaxLayoutWidth
}
set {
_preferredMaxLayoutWidth = newValue
invalidateIntrinsicContentSize()
}
}
override func intrinsicContentSize() -> CGSize {
if let size = image?.size {
let width: CGFloat
let height: CGFloat
if let maxLayoutWidth = preferredMaxLayoutWidth where size.width > maxLayoutWidth {
width = maxLayoutWidth
height = maxLayoutWidth * size.height/size.width
} else {
width = size.width
height = size.height
}
return CGSizeMake(width, height)
}
return CGSizeMake(UIViewNoIntrinsicMetric, UIViewNoIntrinsicMetric)
}
}
the subviews have layout constraints to cell, and the image view update preferredMaxLayoutWidth in cell's layoutSubview body, code like this
override func layoutSubviews() {
super.layoutSubviews()
image🐶.preferredMaxLayoutWidth = CGRectGetWidth(contentView.bounds) - CGFloat(leftMarginImage + rightMarginImage)
}
the result of custom image view is perfect, but cell not recalculate height.
some of system UI like as UILable, UITextView has own instrinsicContentSize implement,which cooperate well with cell
so, the question is how to send information to cell, and let it know subview content size has change and recalculate height
thanks

Resources