Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the top Constraint to be 20 percent of the screen height?

I have an UIImageView in the in the controller's view with a distance of 20% of the screen height from the top of the view. ie I want to set the 20 % of the screen height as the top Constraint of the UIImageView.

Can I implement this without adding a transparent view, which is how I do it now (by setting its height to be 20% of the screen Height), to the container view ?

like image 692
jkdev Avatar asked Dec 08 '22 00:12

jkdev


2 Answers

You can do it in the IB/StoryBoard itself. Since you want the top vertical space between the image view and it's superview to be always 20% of the height of the superview, it is same as setting the image view's height to be 80% of it's superview's height and pinning the bottom of image view to the bottom of it's superview. Hence, you can eliminate that transparent view from being added.

Steps (I'm only considering vertical constraints here):
1. Drag a constraint from the bottom of the image view to the bottom of it's superview.
2. Drag an equal heights constraint from the image view to it's superview. Change the multiplier value of this constraint to 8:10 in the Size Inspector (Screenshot's attached below).

Size Inspector

like image 45
Ganesh Kamath Avatar answered Jan 19 '23 03:01

Ganesh Kamath


Set the top constraint of your UIImageView to bottom of the main view, with constant 0, multiplier 0.2 or 2:10

Reason: the coordinate of your main view bottom is equal to the height of it ( the top most view of the controller).

Edit: Step by Step

  1. Add constraint from top UIImageView to top of the main view (like you normally do).
  2. Go into the detail of that constraint, change main view top to 2nd item if needed, and set it to main view bottom instead of top.
  3. set constraint constant 0, multiplier 0.2 or 2:10
like image 55
Duy Pham Avatar answered Jan 19 '23 04:01

Duy Pham