How to animate a UI Element (label) to the top of the screen?

39 Views Asked by At

The logic I want to use is below, just not sure the exact code to pull this info in order to execute. I think I only need to figure out how to get the label height.

let labelHeight = ???
let screenHeight = self.view.bounds.height
let yPosition = ((screenHeight-labelHeight) / 2 ) - ~20

UIView.animate(withDuration: 3, animations: {
self.movingButton.center.y -= yPosition
}

A main question also is how do I pull the height of a specific label, if there are multiple on the screen.

Thanks!

1

There are 1 best solutions below

0
On

Figured it out after much trial and error!

let buttonHeight = self.movingButton.bounds.height
      let screenHeight = self.view.bounds.height
      let buttonEndHeight = (screenHeight / 2) - (buttonHeight / 2)
        UIView.animate(withDuration: 2, animations: {
            self.movingButton.center.y -= buttonEndHeight - 30

I expect this to place the label 30 pixels below the top of the screen on any device.