Using auto layout to push control down if label extends beyond a certain point

Hi,
Everybody loving the new "forums"? I have very little expectation that anyone will actually see this question.

Okay, so I have a table cell with two objects, a label and a segmented control. Label is pinned to the left margin of the cell and the segmented control is pinned to the right margin of the cell.

These two objects are normally on the same "line" (their verticals centers line up basically).

Under some circumstances, because of either the length of text in the label and/or the width of the device (iPhone 5s, for example), the label will overlap onto the segmented control.

I want the text to be all on one line. I don't want to wrap it. I would like the segmented control to be pushed down below the label (still pinned to the right) when this happens.

First, can auto layout do this? If yes, how? Seems like I need multiple conditions on a constraint to do this, but I can't figure out how to do it and have not found an example that's similar to this.

Any help would be appreciated!

Regards, Patrick

Accepted Reply

You can do this with autolayout and some code.

Principle should be like this:
  • Create a custom class for the cell

  • remove the vertical alignement between label and control

  • add instead a vertical distance constraint, equal to zero to begin with

  • control-drag from the constraint to code, to create an IBOutlet for the constraint, named for instance cellVerticalConstraint

  • When needed, change cellVerticalConstraint.constant to appropriate value (40 for instance).

  • Do the same for the right alignment of the control if needed.

Replies

You can do this with autolayout and some code.

Principle should be like this:
  • Create a custom class for the cell

  • remove the vertical alignement between label and control

  • add instead a vertical distance constraint, equal to zero to begin with

  • control-drag from the constraint to code, to create an IBOutlet for the constraint, named for instance cellVerticalConstraint

  • When needed, change cellVerticalConstraint.constant to appropriate value (40 for instance).

  • Do the same for the right alignment of the control if needed.

Hi Claude,
Thanks for the reply. I already did something similar using a second constraint with a lower priority. When I need the control shifted down, I set the priority higher to activate it. This gives me the result I wanted.
Regards, Patrick