16

The iOS 7 redesign resulted in change of the view hierarchy of UITableViewCells. The content view of the cell was wrapped in a private class called UITableViewCellScrollView.

In iOS 7 UITableViewCellScrollView has clipsToBounds set to YES and UITableViewCellContentView has clipToBounds set to NO.

In iOS 7.1 UITableViewCellScrollView has clipsToBounds set to NO and UITableViewCellContentView has clipToBounds set to NO.

If you call [[self contentView] setClipsToBounds:YES] in iOS 7.1 is does it stick. By the time layoutSubviews is called on the cell UITableViewCellContentView has clipToBounds set to NO again.

[[self contentView] superview] setClipsToBounds:YES] works in iOS 7.1 and sets UITableViewCellScrollView's clipToBounds to YES but this is a very brittle solution.

Overriding layoutSubview on the cell and calling [[self contentView] setClipsToBounds:YES] works but is another fraile solution.

Does anyone know why this change has been made and a more robust solution to it?

Shubhank
  • 21,721
  • 8
  • 65
  • 83
Reid Main
  • 3,394
  • 3
  • 25
  • 42
  • 3
    What about setting `clipsToBounds` to `YES` on the cell itself? It works for me. – Léo Natan Mar 13 '14 at 18:45
  • 2
    That does work as well. Ideally I don't want to tell the table view cell to clip to bounds in case Apple has any plans to make the stuff render outside of the cell because of base UITableViewCell functionality. The content view on the other hand is except to always reside inside the cell and up until 7.1 was always clipping to bounds by default. – Reid Main Mar 14 '14 at 00:08
  • No, setting the `clipsToBounds` to `YES` on the content scroll view is a bug! Assume you want your content to appear outside of the cell. You set the cell and the content view to not clip, and still something is clipping! That's a bug and why Apple removed it. If you want content not to come out of the cell, set the clipping on the cell itself or its content view. But you should be the one to set it, not the system. – Léo Natan Mar 14 '14 at 00:11
  • 1
    Yeah I agree the scroll view clipping is a bug. I would prefer to be able to set clipsToBounds on the content view and have it stick but that appears to have been broken. – Reid Main Mar 14 '14 at 17:27

4 Answers4

10

As discussed in the comments, the only solution right now in iOS7.1 is to set clipsToBounds on the cell itself.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
  • 1
    This doesn't work if you want the cell content to be clipped when it's in "editing" (reordering) mode. Then you need cell.contentView to clip to its bounds and as noted, this keeps getting turned off by iOS, for presumably some good reason. – Christopher Schardt Oct 11 '15 at 05:07
2

It's quite annoying. What I did is add an UIView in the contentView with identical size (and autoresizingMask in width), add the relevant content to this view, and set clipsToBounds to it.

Eino Gourdin
  • 4,169
  • 3
  • 39
  • 67
2

I hit the some problem, and I solved this confused problem by an ugly way finally.

// Create a subclass of UITableView
// Then override setClipsToBounds:
- (void)setClipsToBounds:(BOOL)clipsToBounds {
    [super setClipsToBounds:YES];
}
Veight Zhou
  • 969
  • 1
  • 7
  • 8
1

In iOS 8, checking the cell's "Clip Subviews" in the XIB did not work.

What does work is:

- (void)awakeFromNib {
    [super awakeFromNib];

    // Initialization code
    self.clipsToBounds = YES;
}
Graham Perks
  • 23,007
  • 8
  • 61
  • 83