16

I'm trying to position a view in storyboard to always have it top edge at 1/3 of screen size. I have currently set a constraint to Top Layout Guide, but the constraint's constant is...well it is constant no matter which screen size it is used in, but I want it to be 1/3 of screen size. Can this be done entirely in storyboard? Thanks for answers

Screenshot of current state:

enter image description here

potato
  • 4,479
  • 7
  • 42
  • 99

4 Answers4

28

Create a constraint between the view's top edge and the superview's bottom edge. Set the constraint's constant to zero, and set its multiplier to 1:3. Like this:

enter image description here

Tamás Zahola
  • 9,271
  • 4
  • 34
  • 46
  • This doesn't work for me, when I set the constant to 0, it hugs the view to the bottom of the screen. Also, I can't seem to create a constraint that goes to "Superview.Bottom", it always creates it to go to "Superview.Bottom Margin" – TheJeff Aug 30 '16 at 18:18
4

You can setup it against center or bottom. Here is an example constraints related to center: (1/3 from top = 1.66 from center) contraints

Danil
  • 2,497
  • 2
  • 19
  • 27
4

For anyone trying to make this work programatically I was not able to handle that by 'anchors' style but old NSLayoutConstraint initializer worked:

let constraint = NSLayoutConstraint(item: floatingBtn,
                                    attribute: .centerY,
                                    relatedBy: .equal,
                                    toItem: view,
                                    attribute: .bottom,
                                    multiplier: 1/3,
                                    constant: 0)
constraint.isActive = true

Credits to accepted answer.

Viktor Kucera
  • 6,177
  • 4
  • 32
  • 43
3

You can add empty (clear color) UIView, pin it to the top, and set it's height to be equal to 1/3 of the superview. Then you can pin the top of your yellow view to the bottom of your empty UIView.

almas
  • 7,090
  • 7
  • 33
  • 49