-2

I apologize in advance because this feels simple, but I've been trying to figure this out for a few days and can't figure it out after a lot of searching.

When I check the height of an image I've created programmatically with constraints the size doesn't match it's rendered size.

// Earth Image Setup
view.addSubview(earthImageView)
earthImageView.bottomAnchor.constraint(equalTo: loginTextViewTitle.topAnchor, constant: -standardSpacer).isActive = true
earthImageView.widthAnchor.constraint(equalToConstant: 500).isActive = true
earthImageView.heightAnchor.constraint(equalToConstant: 500).isActive = true
earthImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

// Value of earthImageViewHeight is 276.  Why isn't it 500?
let earthImageViewHeight = earthImageView.frame.size.height

I'm seeing that the 276 comes from the pixels height of the original @1x image. However, how can I get the actual rendered height?

I thought it might have something to do with the points vs pixels, but that doesn't seem to be the case either.

Ben
  • 3,346
  • 6
  • 32
  • 51
  • 1
    Perhaps it’s because you are checking too soon. Constraints are instructions about what to do when layout happens. It hasn’t happened yet. – matt Apr 27 '19 at 19:15
  • Also did you remember to set `translates...` to false? – matt Apr 27 '19 at 19:16
  • Yes, translatesAutoresizingMaskIntoConstraints = false – Ben Apr 27 '19 at 19:23
  • Also did you set the `contentMode` of the image view to `aspectFit` or `aspectFill`? And set its `clipsToBounds` to true? Otherwise you'll see the whole image at its original size. – matt Apr 27 '19 at 19:26
  • Yes and yes. :) – Ben Apr 27 '19 at 19:48
  • Okay so I think my first comment is the one. You're checking at the same moment you set the constraints, but that's too soon; the constraints have not yet been obeyed. – matt Apr 27 '19 at 19:58
  • Awesome. Thanks for leading me in the right direction. I was able to research it and figure it out from there. Not sure why my searching before didn't pop up the approach! – Ben Apr 27 '19 at 22:34

1 Answers1

0

The reason is the view isn't completely laid out yet. Once it's laid out then the you can use the constraints to set up other constraints, which is the question behind the question above.

This post and this post give good explanations of what's going on and how to manage it. For my particular case calling...

self.view.layoutIfNeeded()

...before my animation block started, and then re-adjusting the size of the image I was animating relative to another one that was already laid out.

Ben
  • 3,346
  • 6
  • 32
  • 51
  • I'm sorry, but I think saying `layoutIfNeeded` is silly. There is no need to force layout; layout _will_ happen, at the end of the current transaction. You _know_ what the size of the view _will_ be, because _you_ gave it constraints. It doesn't matter what size it is _now_. The thing to do is just wait until the _next_ transaction, _after_ layout has taken place, before you perform the next step, whatever it is. – matt Apr 27 '19 at 23:36
  • Well your question did not show or describe what "my case" _is_. You have nowhere told us what you _really_ want to do. That, it seems to me, is the thing to ask about. In your answer you say "before my animation block started", but there was no "animation block" in the question. Thus it is unclear how your answer is related to your question, and so it's not really a very good answer. – matt Apr 28 '19 at 21:06
  • It wasn't really a good question in the first place either apparently with the downvotes. : ) – Ben Apr 28 '19 at 22:37