Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>On further investigation (viewing the subview hierarchy of the cell) Interface Builder does place subviews within the cell's <code>contentView</code>, it just doesn't look like it.</p> <p>The root cause of the issue was iOS 6 autolayout. When the cell is placed into editing mode (and indented) the <code>contentView</code> is also indented, so it stands to reason that all subviews within the <code>contentView</code> will move (indent) by virtue of being within the <code>contentView</code>. However, all the autolayout constraints applied by Interface Builder seem to be relative to the <code>UITableViewCell</code> itself, rather than the <code>contentView</code>. This means that even though the <code>contentView</code> indents, the subviews contained within do not - the constraints take charge.</p> <p>For example, when I placed a <code>UILabel</code> into the cell (and positioned it 10 points from the left-hand side of the cell) IB automatically applied a constraint "Horizontal Space (10)". However, this constraint is relative to the <code>UITableViewCell</code> NOT the <code>contentView</code>. This means that when the cell is indented, and the <code>contentView</code> moves, the label stays put as it is complying with the constraint to remain 10 points from the left-hand side of the <code>UITableViewCell</code>.</p> <p>Unfortunately (as far as I am aware) there is no way to remove these IB created constraints from within IB itself, so here is how I solved the problem.</p> <p>Within the <code>UITableViewCell</code> subclass for the cell, I created an <code>IBOutlet</code> for that constraint called <code>cellLabelHSpaceConstraint</code>. You also need an <code>IBOutlet</code> for the label itself, which I called <code>cellLabel</code>. I then implemented the <code>-awakeFromNib</code> method as per below:</p> <pre><code>- (void)awakeFromNib { // ------------------------------------------------------------------- // We need to create our own constraint which is effective against the // contentView, so the UI elements indent when the cell is put into // editing mode // ------------------------------------------------------------------- // Remove the IB added horizontal constraint, as that's effective // against the cell not the contentView [self removeConstraint:self.cellLabelHSpaceConstraint]; // Create a dictionary to represent the view being positioned NSDictionary *labelViewDictionary = NSDictionaryOfVariableBindings(_cellLabel); // Create the new constraint NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-10-[_cellLabel]" options:0 metrics:nil views:labelViewDictionary]; // Add the constraint against the contentView [self.contentView addConstraints:constraints]; } </code></pre> <p>In summary, the above will remove the horizontal spacing constraint which IB automatically added (as is effective against the <code>UITableViewCell</code> rather than the <code>contentView</code>) and we then define and add our own constraint to the <code>contentView</code>.</p> <p>In my case, all the other <code>UILabels</code> in the cell were positioned based upon the position of the <code>cellLabel</code> so when I fixed up the constraint/positioning of this element all the others followed suit and positioned correctly. However, if you have a more complex layout then you may need to do this for other subviews as well. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload