0

I have an iOS app that places an image at a vertical position that is relative to wherever a particular input slider is placed, which can vary depending on the phone and the safe area at the top. The code works fine except on iPhone X.

Here's how I get the vertical position of the slider and then calculate the y position of the image based on my "rate" variable.

var zeroVert = Int(slider8.center.y)
var vertical: Int = zeroVert - 6 - Int(rate*20) 

How do I programmatically adjust this for the safe area?

[update] I just now tried to get the safe area inset (using iPhone Xs max simulator), but it returns zero.

if #available(iOS 11.0, *) {
   insetTop = Int(view.safeAreaInsets.top)
}

[update 2] Now that I think of it, I shouldn't care about the safe area margin as long as the position of the slider is known correctly. The image is just offset from that. So my problem is that I can't seem to fetch the proper position of the slider.

Wayne Henderson
  • 237
  • 3
  • 9

1 Answers1

0

I've found a partial solution. It's based on bsod's answer here: Get safe area inset top and bottom heights

Fetching the position of a screen object is not relevant until the subviews have been layed out.

    var zeroVert: Int = 0

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        zeroVert = Int(slider8.center.y)  // My slider
        placeArrows()  // The function that places the image based on the slider position
    }

This appears to be working. My placeArrows() function is called elsewhere and I think that may now be redundant since it's being called repetitively anytime the subviews are laid out. But it's a brief function and goes by quickly. Good enough for now unless you can help me figure out a better way.

If I don't run the placeArrows() function AFTER I have the valid position of the slider, it uses the initialized value of zeroVert, which I've set to zero to prevent it from appearing on screen.

Wayne Henderson
  • 237
  • 3
  • 9