繁体   English   中英

在UICollectionViewFlowLayout中散布单元格的问题

[英]Problems spreading out cells in UICollectionViewFlowLayout

我目前正在尝试使用动态大小的单元格在UICollectionViewFlowLayout中分隔一些自定义单元格。 我发现的是单元格之间的间距不一致。 我一直在阅读布局,并且已经实现了所有委托方法来控制最小尺寸,但是这些似乎没有效果。 这是现在的视图

在此处输入图片说明

这是我的大小调整代码:

在布局和视图委托中:

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

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

- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    Tag *tag = [[Search sharedManager] tagAtIndexPath:indexPath];
    NSString *labelText = tag[@"Name"];
    CGSize labelTextSize = [labelText sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:8.0f]}];
    return labelTextSize;
}

自定义单元

@implementation TagCellCollectionViewCell
- (id) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.tagBtn.titleLabel.font = [UIFont systemFontOfSize:8.0f];
        self.tagBtn = [UIButton buttonWithType:UIButtonTypeCustom];
       // self.tagBtn.frame = CGRectMake(-frame.size.width/4+20, -frame.size.width/4+5, frame.size.width, frame.size.height);


       [[self.tagBtn layer] setBorderColor:[[UIColor colorWithRed:234.0f/255.0f green:99.0f/255.0f blue:74.0f/255.0f alpha:1.0f] CGColor]];
        [[self.tagBtn layer] setBorderWidth:1.0f];
        self.tagBtn.backgroundColor = [UIColor whiteColor];
        [self.tagBtn setTitleColor:[UIColor colorWithRed:234.0f/255.0f green:99.0f/255.0f blue:74.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];

        [self.contentView addSubview:self.tagBtn];

    }

    return self;
}

@end

我在这里想念的是什么会在每个单元格之间保持一致的空间?

这里的问题是从

- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

与minimumInterimSpacing值重叠。 因为我正在寻找systemfontofsize = 8的东西,所以我需要确保我的临时间距大于该值。 将其设置为上面我设置的5会导致它以奇怪的方式发生冲突,将其设置为15会使单元均匀分布,因为单元之间的像素数大于计算的大小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM