Подтвердить что ты не робот

Позиция заголовка UICollectionView в режиме горизонтального прокрутки с раскладкой

У меня есть iOS UICollectionView с раскладкой Flow с горизонтальным направлением прокрутки. В этой ситуации типичная позиция заголовка находится в левой части ячеек.

Я хочу сделать заголовок в верхней части ячеек секции

Вы знаете, как я могу это сделать?

4b9b3361

Ответ 1

Я думаю, вы можете получить то, что вам нужно, используя стандартное поведение для заголовка.

Установите его (возможно, в viewDidLoad):

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.headerReferenceSize = CGSizeMake(self.collectionView.bounds.size.width, 30);
// other setup
[self.collectionView setCollectionViewLayout:flowLayout];

Затем ответьте на представление заголовка:

#define MY_HEADER_LABEL_TAG 128

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

    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
                                            UICollectionElementKindSectionHeader withReuseIdentifier:@"SectionHeader" forIndexPath:indexPath];
    UILabel *label = (UILabel *)[headerView viewWithTag:MY_HEADER_LABEL_TAG];
    if (!label) {
        label = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 5, 5)];
        label.tag = MY_HEADER_LABEL_TAG;
        label.font = [UIFont boldSystemFontOfSize:12];
        label.textColor = [UIColor darkGrayColor];
        [headerView addSubview:label];
    }

    label.text = [NSString stringWithFormat:@"Section %d", indexPath.section];
    return headerView;
}