iOS 9.0
之后,官方提供了兩個屬性,支持 UICollectionView
的 header
和 footer
懸停效果。
// Set these properties to YES to get headers that pin to the top of the screen and footers that pin to the bottom while scrolling (similar to UITableView).
@property (nonatomic) BOOL sectionHeadersPinToVisibleBounds API_AVAILABLE(ios(9.0));
@property (nonatomic) BOOL sectionFootersPinToVisibleBounds API_AVAILABLE(ios(9.0));
字面意思寫的非常清楚,而這兩個屬性屬于 UICollectionViewFlowLayout
扳缕,所以使用方法也很明顯。
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.sectionHeadersPinToVisibleBounds = YES;
layout.sectionFootersPinToVisibleBounds=YES;
這里需要注意的是别威,UICollectionView
的 header
是由 UICollectionReusableView
創(chuàng)建的第献。請確保 reusableView
只有一個直接子控件,如果 reusableView
直接添加了多個子控件兔港,將不會出現(xiàn)懸停效果庸毫。