Jay Jay - 2 months ago 8
Swift Question

How do I set specific y position, width and height without diving into code?

A designer I'm working with wants to have a UIImageView be at a specific y position, with a specific width/height depending on the iphone size. I've done research and I haven't been able to find anything online that allows me to do so with constraints on the story board. I want to avoid having to use code because throughout the rest of the app there are a lot of similar situations. In the past I've made different storyboards for each phone size to accommodate for the specificity our clients request, but I want to avoid doing this because it feels like bad practice. Is this even possible?


This can be completely handled in the Interface Builder. Follow the steps below.

1) Add your UIImageView to the ViewController

2) Set constraints for Width, Height, Vertically Align and Horizontally Align enter image description here enter image description here

3) You can adjust the Y position in the Size Inspector of the view.

enter image description here

4) Now switch to Assistant Editor and choose 'Preview' and add different screen sizes to see the auto layout in action.

enter image description here

All of this will set a static image size of 250x200, horizontally centered, and vertically at 1/4 of the screen height.