position a view in storyboard at 1/3 of screen size
Asked Answered
C

4

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

Caution answered 19/8, 2015 at 15:49 Comment(4)
YEs this is possible, can you give us a screenshot?Morality
I added a screenshot, nothing special happening there.Caution
Check out this question: #30925023Dribble
FYI, DanLoughney's linked question adjusts SIZE of a view relative to screen; this question is about POSITION of a view on the screen.Shortcake
H
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

Heliotype answered 19/8, 2015 at 16:34 Comment(1)
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"Coraciiform
F
5

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.

Frias answered 24/6, 2018 at 8:33 Comment(0)
H
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

Horselaugh answered 19/8, 2015 at 16:40 Comment(1)
Use a proper rational number for the multiplier like 5:3 :PGallicize
E
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.

Epicrisis answered 19/8, 2015 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.