0

I have a background image and I want to anchor a child UIImage so that its bottom is n% of the height of the background. I deleted all the constraints on the child image and created a constraint

Child Image.Bottom Equal backgroundImage.Bottom

With Constant 0. I was hoping that changing the multiplier to 5 or .2 would move the child UIImage, but the child UIImage doesn't move.

enter image description here

I tried following the instructions in this post position a view in storyboard at 1/3 of screen size but got the same results.

Here is a screenshot that shows what I am trying to do. The yogi image needs to sit on the mat. The bottom of the pose image will always be on the same spot on the map. The mat is baked into the background image.

enter image description here

ohthepain
  • 2,004
  • 3
  • 19
  • 30

1 Answers1

2

You could use a spacer view at the bottom of the screen that is fixed to 20% of the background view, then align the bottom of your image to the top of that.

In code this would be

spacerView.bottomAnchor.constraint(equalTo: backgroundView.bottomAnchor)
spacerView.heightAnchor.constraint(equalTo: backgroundView.heightAnchor, multiplier: 0.2)
imageView.bottomAnchor.constraint(equalTo: spacerView.topAnchor)

You should be able to make these using the Interface Builder too.

David Pettigrew
  • 299
  • 2
  • 10
  • I tried this but my spacerView ends up going all the way up the screen regardless of multiplier. My spacerView is a UIView that I placed in IB. – ohthepain Jan 21 '19 at 20:56
  • To see how to do it in Interface Builder look at @fyodor's answer in https://stackoverflow.com/questions/14935825/autolayout-make-height-of-view-relative-to-half-superview-height – David Pettigrew Jan 22 '19 at 17:35