I have overridden the tableView:indentationLevelForRowAtIndexPath
method in my UITableViewController
derived class as follows:
- (N
Similar to the accepted answer, this is how it can be done in iOS 8 while still using layoutSubviews
with AutoLayout instead.
With _viewConstraints
as an NSMutableArray
ivar and _imageView
as the closest view to the left side of the cell's content view.
- (void)layoutSubviews
{
float indentPoints = indentationLevel * [self indentationWidth];
[self removeConstraints:_viewConstraints];
[_viewConstraints removeAllObjects];
NSDictionary *views = NSDictionaryOfVariableBindings(imageView);
[_viewConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-(%f@1000)-[_imageView]", indentPoints] options:0 metrics:nil views:views]];
[self addConstraints:_viewConstraints];
}
If you have AutoLayout constraints defining everything else in the view then this should push the whole view over the desired indentation amount.
NOTE if using Nib:
You should define the constraint (in this case between _imageView
and its super view) as >=
some number (in my case it was 20). Then the original constraint, and the one being added/removed in layoutSubviews
don't conflict with each other.
You should also consider calling the following in awakeFromNib
[_imageView setTranslatesAutoresizingMaskIntoConstraints:NO]
This is so the old "springs and struts" don't get in the way of the constraints you are adding.
Have you accidentally overridden shouldIndentWhileEditing:
to NO
in your custom table cell class?
If you use a custom cell with constraints, the most convenient way is to set up a constraint that would shift all content from the left edge, and then update it according to the indentation level in your cell subclass.
Assuming indentationWidth
is already set for the cell:
override var indentationLevel: Int {
didSet {
self.leftConstraint.constant = CGFloat(self.indentationLevel) * self.indentationWidth
}
}
The accepted answer could lead to an infinite loop on iOS8 in some cases. So it's better just to override setFrame
of your custom UITableViewCell
and adjust frame there.
-(void)setFrame:(CGRect)frame {
float inset = self.indentationLevel * self.indentationWidth;
frame.origin.x += inset;
frame.size.width -= inset;
[ super setFrame:frame ];
}
Have you added the subviews of your ProjectItemTableViewCell to the cell's contentView? Also, you need to set the subviews' autoresizing masks so that they are repositioned when the contentView size changes.
Constrain to the leading edge of the content view of the custom table cell you have in interface builder. For a simple margin, this seems to suffice. If you need to change the indentation programmatically, see Dean's answer. Or https://stackoverflow.com/a/7321461/5000071.