4

I'm switching to autolayout and I'd like to position views relatively to height of device. How should I setup constraints to satisfy such condition.

I have nice layout for iPhone 5 but for iPhone 6Plus I'd like to move "red" to position of "gray":

iPhone 5s layout iPhone 6Plus layout

All my current constrains:

Constrains Xcode

Fishman
  • 1,737
  • 1
  • 25
  • 42

3 Answers3

1

One idea might be to place the username, password and login items on a uiView with a clearBackground and then create a constraint for that view and its superview and create an outlet to it. You could then detect which phone you are using in code and modify the constraints programatically in willLayoutSubviews.

if ((int)[[UIScreen mainScreen] bounds].size.height == 736)
{
    // This is iPhone 6+ screen
    myConstraint.constant = 150;

} else if ((int)[[UIScreen mainScreen] bounds].size.height == 568) {
    // This is iPhone 5s screen
    frameRateLabelHeightConstraint.constant = 100;
}

There will be a better way to do this in Autolayout no doubt but I do find it a bit confusing so I have used this method in the past.

Md1079
  • 1,360
  • 1
  • 10
  • 28
  • And when do you make such updates? In viewDidLoad: view.height is always 568 no matter what device is used. In viewWillLayoutSubviews it has such information, but change in constraint doesn't affect view. – Fishman Jan 21 '15 at 16:36
  • It certainly should affect the views, If you change the constant value of a constraint it will alter the view accordingly. in my experience anyway. I like Pranav's answer though, set the vertical constraint of the username box such that it has lower priority than the other constraints. check out his supplied code it's a good way of doing it. – Md1079 Jan 21 '15 at 16:54
1

Here is a sample project uploaded @ One Drive. Here are the sample outputs on various versions of iPhone Simulators...iPhone 4/4SiPhone 5/5SiPhone 6iPhone6+

Pranav Jaiswal
  • 3,752
  • 3
  • 32
  • 50
0

As I see you want to put your form to the center of view. So I think the best solution will be to put your form elements on another transparent view and add to this view centerX aligment constraint and centerY aligment constraint.

you can check another way here

Sergey Pekar
  • 8,555
  • 7
  • 47
  • 54
  • No I don't want to center it (it's slightly closer to top). It would be easiest way. But let's say I want to put red rectangle always in 1/3 of screen height? – Fishman Jan 21 '15 at 11:24
  • Another good way is to create empty view with height equals 1/3 of screen height. then set it's width equal to parents view width. After set to this view aspect ratio constraint. and put your view with form below your empty view. Accordig to it's constraints empty view should resize to 1/3 height of screen and if you form will be below of the empty view the from should be on the right position. Added link to a tutorial to my answer – Sergey Pekar Jan 21 '15 at 11:29
  • How can I add constraint 'aspect ratio' between child view and its container? – Fishman Jan 21 '15 at 11:35
  • You need to add this constraint to view only. – Sergey Pekar Jan 21 '15 at 12:51