【问题标题】:UICollectionViewCell higher y than UICollectionviewUICollectionViewCell 比 UICollectionview 高 y
【发布时间】:2018-04-01 21:06:46
【问题描述】:

我的应用中有一个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 更高。

我已经绞尽脑汁好几个小时了,但我似乎无法弄清楚为什么会这样。有什么想法吗?

【问题讨论】:

  • collectionView 根据代码有一个 clearColor 不是黄色的?
  • @Sh_Khan 抱歉,我刚刚将屏幕截图设为黄色。
  • 它不是全屏也根据代码可以使用更新代码也可以使用您在其中创建集合的方法
  • @user4992124 在这里你根据 tabbarcontroller 设置单元格高度,因此它高于单元格高度,只需使用单元格设置高度
  • 如何将 clipsTobounds 设置为 true ,对于 UICollectionView

标签: ios objective-c uicollectionview uikit


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多