Auto Layout in UICollectionViewCell not working

后端 未结 4 481
灰色年华
灰色年华 2020-11-28 19:44

I have a simple UICollectionView with cells that have a single UITextView. The UITextView is constrained to the edges of the cell, so they should stay the same size as the c

相关标签:
4条回答
  • 2020-11-28 20:03

    Equivalent Swift Code :

    override var bounds: CGRect {
        didSet {
          contentView.frame = bounds
        }
    }
    
    0 讨论(0)
  • 2020-11-28 20:10

    Swift 2.0:

    cell.contentView.frame = cell.bounds
    cell.contentView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
    
    0 讨论(0)
  • 2020-11-28 20:16

    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]
    
    0 讨论(0)
  • 2020-11-28 20:19

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题