In the optional UICollectionView (header), I have a multi-line label that I want to trim to 3 lines.

When the user backs down anywhere above the header (optional) view, I want to switch the UILabel to 0 lines so that all the text is displayed, and accordingly increase the size of the additional collectionView (preferably animated). Here's what happens after clicking on the title:

Here is my code:
// MyHeaderReusableView.m // my gesture recognizer action - (IBAction)onHeaderTap:(UITapGestureRecognizer *)sender { self.listIntro.numberOfLines = 0; // force -layoutSubviews to run again [self setNeedsLayout]; [self layoutIfNeeded]; } - (void)layoutSubviews { [super layoutSubviews]; self.listTitle.preferredMaxLayoutWidth = self.listTitle.frame.size.width; self.listIntro.preferredMaxLayoutWidth = self.listIntro.frame.size.width; [self layoutIfNeeded]; CGFloat height = [self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; self.frame = ({ CGRect headerFrame = self.frame; headerFrame.size.height = height; headerFrame; }); NSLog(@"height: %@", @(height)); }
When I register height at the end of layoutSubviews , its value is 149, while the label is truncated and numberOfLines is 3. After clicking headerView, setting numberOfLines to 0 and forcing the layout, the height is then written as 163.5. Fine!
The only problem is that the whole header file does not grow and the cells are not dumped.
How can I dynamically change the height of my additional collectionView (preferably animated)?
I know UICollectionViewFlowLayout headerReferenceSize and collectionView:layout:referenceSizeForHeaderInSection: but I'm not quite sure how I will use them in this situation.
ios objective-c autolayout uicollectionview
djibouti33
source share