1
This commit is contained in:
@@ -20,6 +20,9 @@ static NSString * const kKBVipReviewListCellId = @"kKBVipReviewListCellId";
|
||||
@property (nonatomic, assign) NSInteger selectedIndex; // 当前选中的方案索引
|
||||
@property (nonatomic, strong) UIButton *closeButton; // 当前选中的方案索引
|
||||
@property (nonatomic, strong) UIImageView *bgImageView; // 全屏背景图
|
||||
// Header 自适应测量
|
||||
@property (nonatomic, strong) KBVipPayHeaderView *sizingHeader;
|
||||
@property (nonatomic, assign) CGFloat headerHeight;
|
||||
|
||||
@end
|
||||
|
||||
@@ -60,6 +63,8 @@ static NSString * const kKBVipReviewListCellId = @"kKBVipReviewListCellId";
|
||||
make.width.height.mas_equalTo(36);
|
||||
}];
|
||||
|
||||
// 预计算 Header 高度(由内部约束决定)
|
||||
self.headerHeight = [self kb_calcHeaderHeightForWidth:KB_SCREEN_WIDTH];
|
||||
[self.collectionView reloadData];
|
||||
}
|
||||
|
||||
@@ -75,6 +80,22 @@ static NSString * const kKBVipReviewListCellId = @"kKBVipReviewListCellId";
|
||||
if (cell) { [cell applySelected:YES animated:NO]; }
|
||||
}
|
||||
|
||||
#pragma mark - Header Height Calc
|
||||
- (CGFloat)kb_calcHeaderHeightForWidth:(CGFloat)width {
|
||||
if (width <= 0) { width = KB_SCREEN_WIDTH; }
|
||||
if (!self.sizingHeader) {
|
||||
self.sizingHeader = [[KBVipPayHeaderView alloc] initWithFrame:CGRectMake(0, 0, width, 1)];
|
||||
}
|
||||
// 更新目标宽度并触发布局
|
||||
self.sizingHeader.bounds = CGRectMake(0, 0, width, self.sizingHeader.bounds.size.height);
|
||||
[self.sizingHeader setNeedsLayout];
|
||||
[self.sizingHeader layoutIfNeeded];
|
||||
CGSize size = [self.sizingHeader systemLayoutSizeFittingSize:CGSizeMake(width, UILayoutFittingCompressedSize.height)
|
||||
withHorizontalFittingPriority:UILayoutPriorityRequired
|
||||
verticalFittingPriority:UILayoutPriorityFittingSizeLevel];
|
||||
return MAX(1, ceil(size.height));
|
||||
}
|
||||
|
||||
#pragma mark - Action
|
||||
- (void)onTapClose{
|
||||
[self.navigationController popViewControllerAnimated:true];
|
||||
@@ -154,8 +175,10 @@ static NSString * const kKBVipReviewListCellId = @"kKBVipReviewListCellId";
|
||||
|
||||
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
|
||||
if (section == 0) {
|
||||
// 头部高度 = 484 + 导航栏高度
|
||||
return CGSizeMake(KB_SCREEN_WIDTH, 474 + KB_NAV_TOTAL_HEIGHT);
|
||||
// 动态返回测量好的 Header 高度
|
||||
CGFloat w = collectionView.bounds.size.width ?: KB_SCREEN_WIDTH;
|
||||
if (self.headerHeight <= 1) { self.headerHeight = [self kb_calcHeaderHeightForWidth:w]; }
|
||||
return CGSizeMake(w, self.headerHeight);
|
||||
}
|
||||
return CGSizeZero;
|
||||
}
|
||||
@@ -180,6 +203,8 @@ static NSString * const kKBVipReviewListCellId = @"kKBVipReviewListCellId";
|
||||
if (!_collectionView) {
|
||||
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
|
||||
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
|
||||
// 每次宽度变化时让布局失效,便于 header 重算高度
|
||||
layout.sectionHeadersPinToVisibleBounds = NO;
|
||||
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
|
||||
_collectionView.backgroundColor = [UIColor clearColor];
|
||||
_collectionView.dataSource = self;
|
||||
@@ -204,4 +229,16 @@ static NSString * const kKBVipReviewListCellId = @"kKBVipReviewListCellId";
|
||||
}
|
||||
return _closeButton;
|
||||
}
|
||||
|
||||
- (void)viewDidLayoutSubviews {
|
||||
[super viewDidLayoutSubviews];
|
||||
// 宽度变化时重算 Header 高度并刷新布局
|
||||
CGFloat w = self.collectionView.bounds.size.width ?: KB_SCREEN_WIDTH;
|
||||
CGFloat newH = [self kb_calcHeaderHeightForWidth:w];
|
||||
if (fabs(newH - self.headerHeight) > 0.5) {
|
||||
self.headerHeight = newH;
|
||||
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
|
||||
[layout invalidateLayout];
|
||||
}
|
||||
}
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user