-1

Let's supose I have a UIImageView at y=10 while the screen is height:100.

What constraints do I have to set if I want the image to be always for example at y:10% of the parent? Parent height:100 - UIImage y:10 Parent height:200 - UIImage y:20 Parent height:320 - UIImage y:32

Is there a way to do this without coding? (only constraints in Interface Builder)? Hint: before autolayout this was really easy.

Thanks!

jobima
  • 5,790
  • 1
  • 20
  • 18
  • Hint 2: `[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:0.1 constant:0.0];` – BooRanger Mar 06 '15 at 15:38
  • 3
    Hint: Before autolayout this was a mess when working with rotation/other screen sizes! – ullstrm Mar 06 '15 at 15:38
  • Damn I'm wrong! Ignore ME! – BooRanger Mar 06 '15 at 15:53

1 Answers1

4

You can do this in interface builder using spacer views.

  1. Put a hidden UIView in your parent view with 0 top spacing to it's superview.
  2. Make the hidden UIView have equal height to the parent view with a multiplier of 0.1 so it is always 10% of the parent view's height.
  3. Make your UIImageView have 0 top spacing to the hidden UIView.

This should make your UIImageView y position always 1/10th of the height of the parent view.

dan
  • 9,695
  • 1
  • 42
  • 40
  • This works but it seems a bit tricky. Isn't there a more standard way? Thanks – jobima Mar 06 '15 at 16:04
  • 1
    @jobima, it seems to be odd but it is the way it works now...even Apple use similar trick in their guide https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AutoLayoutbyExample/AutoLayoutbyExample.html#//apple_ref/doc/uid/TP40010853-CH5-SW8 – Max Komarychev Mar 06 '15 at 16:10