我正在使用 UICollectionLayoutListConfiguration 来制作像 UITableView 一样的 UICollectionView 布局。
但 UICollectionLayoutListConfiguration 中唯一支持的页脚视图模式似乎是 section 页脚视图。当该部分在屏幕上滚动时,它将固定到视图。
@property (nonatomic) UICollectionLayoutListFooterMode footerMode;
typedef NS_ENUM(NSInteger, UICollectionLayoutListFooterMode) {
/// No footers are shown
UICollectionLayoutListFooterModeNone,
/// Uses supplementary views of kind UICollectionElementKindSectionFooter to show footers
UICollectionLayoutListFooterModeSupplementary,
} API_AVAILABLE(ios(14.0), tvos(14.0), watchos(7.0));
但我希望页脚视图的行为类似于 UITableView 的 tableFooterView ,它与任何部分无关,也不会固定屏幕。如果我使用 UICollectionViewCompositionalLayoutConfiguration 而不是 UICollectionLayoutListConfiguration ,将会有一个 borderSupplementaryItems 属性来执行此操作。但 UICollectionLayoutListConfiguration 似乎对此没有边界SupplementaryItems。
有什么方法可以使用 UICollectionLayoutListConfiguration 制作 tableFooterView 布局吗?
@interface ListFooterViewController:UIViewController
@end
@interface ListFooterViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@end
@implementation ListFooterViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupCollectionView];
}
- (void)setupCollectionView {
// 创建列表布局配置
UICollectionLayoutListConfiguration *listConfig = [[UICollectionLayoutListConfiguration alloc] initWithAppearance:UICollectionLayoutListAppearancePlain];
listConfig.footerMode = UICollectionLayoutListFooterModeSupplementary;
// 创建并配置 UICollectionViewCompositionalLayout
UICollectionViewCompositionalLayout *layout = [[UICollectionViewCompositionalLayout alloc] initWithSectionProvider:^NSCollectionLayoutSection * _Nullable(NSInteger section, id<NSCollectionLayoutEnvironment> _Nonnull layoutEnvironment) {
// 创建列表布局
NSCollectionLayoutSection *listSection = [NSCollectionLayoutSection sectionWithListConfiguration:listConfig layoutEnvironment:layoutEnvironment];
// 添加页脚
NSCollectionLayoutBoundarySupplementaryItem *footerItem = [NSCollectionLayoutBoundarySupplementaryItem boundarySupplementaryItemWithLayoutSize:[NSCollectionLayoutSize sizeWithWidthDimension:[NSCollectionLayoutDimension fractionalWidthDimension:1] heightDimension:[NSCollectionLayoutDimension absoluteDimension:50]] elementKind:UICollectionElementKindSectionFooter alignment:NSRectAlignmentBottom];
listSection.boundarySupplementaryItems = @[footerItem];
return listSection;
}];
// 创建并配置 UICollectionView
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
self.collectionView.backgroundColor = [UIColor systemBackgroundColor];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.view addSubview:self.collectionView];
// 注册单元格和页脚视图
[self.collectionView registerClass:[UICollectionViewListCell class] forCellWithReuseIdentifier:@"cell"];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor whiteColor];
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
footer.backgroundColor = [UIColor redColor];
return footer;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Selected item at indexPath: %@", indexPath);
}
@end
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
ListFooterViewController* vc = [ListFooterViewController new];
[self.navigationController pushViewController:vc animated:YES];
}