Automatic UICollectionView Header Calibration

I am trying to create a detailed screen for a list of applications. Here's what the detailed screen looks like:

detail screen

This is UICollectionViewControllerwith a headline. The header contains 2 objects UILabeland an object UITextView. The layout of these objects is controlled vertically UIStackView. UIViewUsed to set the white background.

I had some difficulty determining the height of this UICollectionReusableViewat runtime. Any advice is appreciated.

+5
source share
2 answers

This is a bit of a hack, but it seems to work.

    // showhere to keep a reference
    UICollectionReusableView * _cachedHeaderView;


    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView 
               viewForSupplementaryElementOfKind:(NSString *)kind 
                                     atIndexPath:(NSIndexPath *)indexPath{

        if(!_cachedHeaderView){
            // dequeue the cell from storyboard
            _cachedHeaderView = 
         [collectionView dequeueReusableCellWithReuseIdentifier:@"header_cell" 
                                                   forIndexPath:indexPath];

            // set captions/images on the header etc...

            // tell the collectionview to redraw this section
          [self.collectionView reloadSections:[NSIndexSet 
                            indexSetWithIndex:indexPath.section]];
        }

        return _cachedHeaderView;
    }


        - (CGSize)collectionView:(UICollectionView *)collectionView 
                          layout:(UICollectionViewLayout*)collectionViewLayout        
 referenceSizeForHeaderInSection:(NSInteger)section{

        // once there is a reference ot the view, use it to figure out the height
        if(_cachedHeaderView){
            CGSize size = 
[_cachedHeaderView systemLayoutSizeFittingSize:collectionView.bounds.size 
                 withHorizontalFittingPriority:UILayoutPriorityRequired 
                       verticalFittingPriority:UILayoutPriorityDefaultLow];
            return size;

        }

        // a placeholder value just to get the dequeueReusableCellWithReuseIdentifier to work 
        return CGSizeMake(collectionView.bounds.size.width, 100);
    }

+2
source

UICollectionViewReusableView XIB.

  • referenceSizeForHeaderInSection.
  • , .
  • , .
  • .
  • , .
  • setNeedsLayout layoutIfNeeded

: , , . .

# 2: .

PureLayout .

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

    let header = CustomHeaderView()

        header.isHidden = true;
        self.view.addSubview(header)
        header.autoPinEdge(toSuperviewEdge: .leading)
        header.autoPinEdge(toSuperviewEdge: .trailing)
        header.autoPin(toTopLayoutGuideOf: self, withInset: 0)
        header.setupHeader(withData: self.data)
        header.setNeedsLayout()
        header.layoutIfNeeded()
        header.removeFromSuperview()

        return header.frame.size
}
0

Source: https://habr.com/ru/post/1652817/


All Articles