2

So I was working on trying to make a simple animation of a button in my app. I have a button in a container centered on the view using auto layout.

I am looking to move the button upwards in the view and stay horizontally centered

So far I got this

UIView.animateWithDuration(1.0, animations:
{
     self.ButtonViewController.frame = CGRect(x: (SOME VALUE), y: 0, width: 600, height: 113)
})

What value could I use for x to keep in centered in the view for all screen sizes?

Thanks!

1
  • I'm glad you accepted my answer, many times problems such as this are solved instantly by maths. ;)
    – andreszs
    Commented Feb 4, 2015 at 14:07

1 Answer 1

2

Why don't you get the screen width first and use that value to calculate the button's x position accordingly?

If you manage to get the current screen width into the screenWidth variable somehow, and assuming your button width is fixed to 600, then try something like:

self.ButtonViewController.frame = CGRect(x: ((screenWidth/2)-300), y: 0, width: 600, height: 113)
1
  • 1
    Perfect! That was exactly what I was trying to do but I just had the math wrong. Thanks! Commented Feb 4, 2015 at 5:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.