简体   繁体   English

UICollectionView单元格大小ContentView问题

[英]UICollectionView cell size ContentView Issue

I have a UICollectionViewCell in storyboard the cell. 我在单元中的情节提要中有一个UICollectionViewCell。 The cell size is 45 for first 6 cells and 50 for the last cell. 前6个单元格的大小为45,最后一个单元格的大小为50。

Storyboard cell size has width = 60. 故事板单元大小的宽度为60。

The following issue occurs when running on iOS Simulator 4s. 在iOS Simulator 4s上运行时,会发生以下问题。

在此处输入图片说明

If you see the cell at location 2,4,6 there is a red background of UICollectionView. 如果您在位置2、4、6处看到该单元格,则UICollectionView的背景为红色。

I get the following in layoutSubView of the subclass. 我在子类的layoutSubView中得到以下内容。

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. 另外,在aakeakefromnib中添加了无法调整大小的功能。

-(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 在ViewController中添加以下内容也不起作用

-(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. 演示项目可以在URL上使用一段时间

On further investigation I found was because of scaling simulator. 在进一步调查中,我发现是因为缩放模拟器。 The screens are with a 4s simulator. 屏幕带有4s模拟器。 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 100%=>结果正常75%=>出现红色背景线50%=>结果正常33%=>结果确定25%=>红色背景线出现

在此处输入图片说明 在此处输入图片说明 在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

try to set the constraints of the collectionView like this 尝试像这样设置collectionView的约束 在此处输入图片说明

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

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