Simple way to change the position of UIView?

Seunghoon picture Seunghoon · Mar 1, 2011 · Viewed 125k times · Source

I change the position of a UIView with following codes without changing size of the view.

CGRect f = aView.frame;
f.origin.x = 100; // new x
f.origin.y = 200; // new y
aView.frame = f;

Is there more simple way to change only the view position?

Answer

TomSwift picture TomSwift · Mar 1, 2011
aView.center = CGPointMake(150, 150); // set center

or

aView.frame = CGRectMake( 100, 200, aView.frame.size.width, aView.frame.size.height ); // set new position exactly

or

aView.frame = CGRectOffset( aView.frame, 10, 10 ); // offset by an amount

Edit:

I didn't compile this yet, but it should work:

#define CGRectSetPos( r, x, y ) CGRectMake( x, y, r.size.width, r.size.height )

aView.frame = CGRectSetPos( aView.frame, 100, 200 );