Auto Layout in UICollectionViewCell not working

若如初见. 提交于 2019-11-26 23:44:24
ryanthon

Well, I just looked on the iOS developer forums. Apparently, this is a bug with the iOS 8 SDK running on iOS 7 devices. The workaround is to add the following to your subclass of UICollectionViewCell:

- (void)setBounds:(CGRect)bounds {
    [super setBounds:bounds];
    self.contentView.frame = bounds;
}
override var bounds: CGRect {
    didSet {
      contentView.frame = bounds
    }
}

Equivalent Swift Code :

override var bounds: CGRect {
    didSet {
      contentView.frame = bounds
    }
}

Here is the solution, if you are not subclassing UICollectionViewCell. Just add this two lines under your cellForItemAtIndexPath: after dequeueReusableCellWithReuseIdentifier:

Obj-C

[[cell contentView] setFrame:[cell bounds]];
[[cell contentView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

Swift - 2.0

cell.contentView.frame = cell.bounds
cell.contentView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

Swift 2.0:

cell.contentView.frame = cell.bounds
cell.contentView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!