简单的方法来改变UIView的位置?
问题描述:
我使用以下代码更改UIView的位置,而不更改视图的大小。
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?
答
aView.center = CGPointMake(150, 150); // set center
或
aView.frame = CGRectMake( 100, 200, aView.frame.size.width, aView.frame.size.height ); // set new position exactly
或
aView.frame = CGRectOffset( aView.frame, 10, 10 ); // offset by an amount
编辑:
我还没有编译它,但它应该可以工作:
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 );