Calculate EdgeInsets instead of hard code?

The book has us hard code the edge insets for the list view controller:

segue.destination.additionalSafeAreaInsets = UIEdgeInsets(top: 0, left: 0, bottom: 160, right: 0)

However, this seems brittle to me. If the size of the button or stackview ever change, we have to remember to update this value, and use trial and error to get it to be the right size.

It seems like a better approach would be to calculate the bottom inset value based on the height of the button and stack view (and the spacing between them). I tried to do that using code like this:

//calculate the bottom inset value
let bottom = stackView.bounds.height + addMoodButton.bounds.height + 20
// and now use it to set the bottom safe area insets
segue.destination.additionalSafeAreaInsets = UIEdgeInsets(top: 0, left: 0, bottom: bottom, right: 0)

But this doesn’t work. The calculated value is not big enough for the content to properly scroll out from under the button. I presume this is because one of the items (bounds or insets) is using raw pixels, whereas the other is using points. But I’m not sure which. This leads me to 2 questions:

  1. Simply, what is the best way to calculate the bottom insets value instead of hard coding the value.
  2. Teach me to fish … how could I have figured that out from the developer documentation? I looked up the docs for several objects that are used here (Bounds, CGRect, UIEdgeInsets), and none of them give any indication if the values they use are points or pixels. How does a developer find these kinds of things out? I find the documentation to be significantly lacking in these areas.
1 Like

So, I was able to answer question 1 on my own …

My problem was using the height of the stackView, when I should’ve been using the height of the VisualEffectView. The screenshot of the interface builder below shows why … The StackView is inside the VisualEffectView, and does not fill it’s full height.

So, I added an outlet for the VisualEffectView (I called it blurView) . Using it’s height, I can calculate the bottom inset value and it works correctly:

let bottom = blurView.bounds.height + addMoodButton.bounds.height + 20
segue.destination.additionalSafeAreaInsets = UIEdgeInsets(top: 0, left: 0, bottom: bottom, right: 0)

But, my second question remains … Is there somewhere in the docs that specifies whether these objects are using point values or raw pixel values?

1 Like

They’re points. See page 59 (or look up “points” in the index).