UICollectionView 单元格大小 ContentView 问题 [英] UICollectionView cell size ContentView Issue

查看:36
本文介绍了UICollectionView 单元格大小 ContentView 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单元格的情节提要中有一个 UICollectionViewCell.前 6 个单元格的单元格大小为 45,最后一个单元格的单元格大小为 50.

故事板单元格的宽度为 60.

在 iOS Simulator 4s 上运行时出现以下问题.

如果您在位置 2、4、6 处看到单元格,则 UICollectionView 的背景为红色.

我在子类的 layoutSubView 中得到以下内容.

帧 = {{0, 0}, {50, 60}}帧 = {{0, 0}, {45, 60}}帧 = {{0, 0}, {45, 60}}帧 = {{0, 0}, {45, 60}}帧 = {{0, 0}, {45, 60}}帧 = {{0, 0}, {45, 60}}帧 = {{0, 0}, {45, 60}}- (void)layoutSubviews{[超级布局子视图];NSLog(@"Frame = %@",NSStringFromCGRect(self.bounds));self.contentView.frame = self.bounds;}

另外,在awakefromnib中添加了调整大小,但不起作用.

-(void)awakeFromNib{[超级awakeFromNib];self.contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth ;}

上面打印出来的结果是正确的,尺寸是完美的.

在 ViewController 中添加以下内容也不起作用

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{返回 0.0;}-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{返回 0.0;}

演示项目可在

解决方案

尝试像这样设置collectionView的约束

I have a UICollectionViewCell in storyboard the cell. The cell size is 45 for first 6 cells and 50 for the last cell.

Storyboard cell size has width = 60.

The following issue occurs when running on iOS Simulator 4s.

If you see the cell at location 2,4,6 there is a red background of UICollectionView.

I get the following in layoutSubView of the subclass.

Frame = {{0, 0}, {50, 60}}
Frame = {{0, 0}, {45, 60}}
Frame = {{0, 0}, {45, 60}}
Frame = {{0, 0}, {45, 60}}
Frame = {{0, 0}, {45, 60}}
Frame = {{0, 0}, {45, 60}}
Frame = {{0, 0}, {45, 60}}


- (void)layoutSubviews
{
    [super layoutSubviews];

    NSLog(@"Frame = %@",NSStringFromCGRect(self.bounds));
    self.contentView.frame = self.bounds;
}

Also, added the resizing in awakefromnib which did not work.

-(void)awakeFromNib{
    [super awakeFromNib];

   self.contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth ;
}

The above result which is printed is correct, the sizes are perfect.

Adding the following in ViewController also did not work

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

A demo project is available for some time at url.

On further investigation I found was because of scaling simulator. The screens are with a 4s simulator. I don't know if on device it will cause any issue.

100% => result Ok 75% => red background lines appear 50% => result Ok 33% => result Ok 25% => red background lines appear

解决方案

try to set the constraints of the collectionView like this

这篇关于UICollectionView 单元格大小 ContentView 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆