UICollectionView View Header and Footer

Hi, I wonder how we achieve this?

in a uitableview we could just do tableView.tableHeaderView = SomeView;ortableView.tableFooterView = SomeView;

But I am wondering how to do the same for a UICollectionView.

PS: Not Header and Footer

+5
source share
5 answers

Try it ...

First register class in ViewDidLoad

registerClass(myFooterViewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "Footer")

then use this method

override func collectionView(collectionView: UICollectionView,   viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

  switch kind {

    case UICollectionElementKindSectionHeader:

        let header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as! UICollectionReusableView

        header = SomeView
        return header

    case UICollectionElementKindSectionFooter:
        let footer = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Footer", forIndexPath: indexPath) as! UICollectionReusableView

        footer= SomeView 
        return footer

    default:

       print("anything")
    }
}

Hope this helps ...

+7
source

UITableViewhave global headers tableHeaderViewand tableFooterViewbut UICollectionViewno global headers, so you have to use section headers.

+5

, headerFooter: UICollectionReusableView, , , viewcontroller :

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let sectionView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "FooterView", for: indexPath) as! CollectionReusableView

    return sectionView
}

:

class CollectionReusableView: UICollectionReusableView {

    @IBAction func ButtonClick(_ sender: Any) {
        print("Click 1st")
    }
    @IBAction func ButtonClick2(_ sender: Any) {
        print("Click 2nd")
    }    
}
0

. , .

        CGFloat headerHeight = 100;
        UIView *headerView = ({
            UIView *view = [[UIView alloc] init];
            view.frame = CGRectMake(0, -headerHeight, collectionView.frame.size.width,headerHeight);
            view;
        });
        [collectionView addSubview:headerView];

        CGFloat footerHeight = 100;
        UIView *footerView = ({
            UIView *view = [[UIView alloc] init];
            view.frame = CGRectMake(0, collectionView.frame.size.height, collectionView.frame.size.width, footerHeight);
            view;
        });

        self.footerView = footerView;
        [collectionView addSubview:footerView];
        collectionView.contentInset = UIEdgeInsetsMake(headerHeight, 0, footerHeight, 0);

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
       if (scrollView.contentSize.height > scrollView.frame.size.height) {
          CGRect frame = self.footerView.frame;
          frame.origin.y = scrollView.contentSize.height;
          self.footerView.frame = frame;
       }
}
0

- collectionView.contentInset.top = height collectionView:

let header = UIView(frame: CGRect(x: 0, y: -height, width:     collectionView.frame.width, height: height))
// ... customise view, add subviews
collectionView.addSubview(header)

. autolayout . . collectionView -

0

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


All Articles