UICollectionViewCell比UICollectionview高y

时间:2018-04-01 21:06:47

标签: ios objective-c uicollectionview uikit

我的应用中有UICollectionView,我使用以下代码初始化:

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
                                                     collectionViewLayout:layout];
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView setPagingEnabled:YES];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"normalCell"];
[self.collectionView setShowsHorizontalScrollIndicator:NO];
[self.collectionView setShowsVerticalScrollIndicator:NO];
[self.view addSubview:self.collectionView];

然后我添加了所有协议方法:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"normalCell" forIndexPath:indexPath];

    if (indexPath.row == 0) {
        cell.backgroundColor = [UIColor blueColor];
    } else if (indexPath.row == 1) {
        cell.backgroundColor = [UIColor greenColor];
    } else if (indexPath.row == 2) {
        cell.backgroundColor = [UIColor yellowColor];
    }

    return cell;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    UIEdgeInsets safeInsets = self.tabBarController.view.safeAreaInsets;

    NSLog(@"Top: %f", safeInsets.top);

    self.collectionView.frame = CGRectMake(0, safeInsets.top, self.view.frame.size.width, self.view.frame.size.height-safeInsets.top);
    [self.collectionView reloadData];
}

现在,UICollectionView完全放在我的视野中。但是,UICollectionViewCell的启动率远高于UICollectionView

在下面的屏幕截图中,您可以看到该行为。蓝色,您可以看到UICollectionViewCell,黄色,您可以看到UICollectionView。你可以看到它似乎有一个更高的y坐标,然后是UICollectionView

enter image description here

我现在已经筋疲力尽了几个小时,但我似乎无法弄清楚为什么要这样做。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这里你根据tabbarcontroller设置单元格高度,所以它高于单元格高度,只需用单元格设置高度。