1

Without using code, I'm basically trying to achieve the "Desired outcome" in this picture:

enter image description here

I want the constraint on the "Hello!" label to shrink its length when the screen height is shorter. As you can see in "Actual result", the Button in the bottom is off the screen. I want the image view to have fixed width and height.

I know I can create IBOutlets for constraints and doing it by code like this kind of posts suggest (autolayout - make height of view relative to half superview height), but I'm trying to avoid using code as much as possible.

Side question: If there's no way to do this in IB, what are the best ways to do this in code?

Thanks for the help!

Community
  • 1
  • 1
Genki
  • 3,055
  • 2
  • 29
  • 42

1 Answers1

5

Yes. The easiest way to have this kind of "split height" constraint is to put a "header" view between your image view and the top of the screen, and embed your "Hello" label inside this new header view. Then add a constraint to keep the "Hello" label vertically centered inside the header view.

To keep the header view the right size, add constraints to keep the top of the header view pinned to the top of the screen, and the bottom of the header view to the top of the image view.

Then you'll just add constraints to keep the button and image view pinned to the bottom of the screen. (Or, see comment from Sulthan, below.)

In Xcode 5.1 (in beta), there is UI to make more general constraints, including the ones you want, but you have to do it like this or in code in earlier versions.

Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
  • 2
    I would go with fixing the image Y relative to the center of the superview first and then also use a "footer" view because the vertical constraints around the button will suffer from the same problem. – Sulthan Feb 28 '14 at 22:08