Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS SDK: Moving the button into the center of screen by code

I want to move a button to the center of the screen by code. I saw somewhere it is just few lines of code, but could not find them.

like image 569
DavidNg Avatar asked Jul 17 '12 17:07

DavidNg


People also ask

How to center a button in xcode?

To create constraints select the button and click the Align icon in the auto layout menu. A popover menu will appear, check both “Horizontal in container” and “Vertically in container” options to center the button on the screen.


1 Answers

This centers the button in its superview:

CGRect bounds = button.superview.bounds;
button.center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
like image 163
Matt Wilding Avatar answered Sep 20 '22 18:09

Matt Wilding