Easy way to reposition a UIView?

I change the position of the UIView with the following codes without resizing the view.

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

Is there an easier way to change just the position of a view?

+70
iphone uiview
Mar 01 '11 at 10:18
source share
12 answers
 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 have not compiled 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 ); 
+223
Mar 01 2018-11-11T00:
source share

I had the same problem. I made a simple UIView category that fixes this.

.h

 #import <UIKit/UIKit.h> @interface UIView (GCLibrary) @property (nonatomic, assign) CGFloat height; @property (nonatomic, assign) CGFloat width; @property (nonatomic, assign) CGFloat x; @property (nonatomic, assign) CGFloat y; @end 

.m

 #import "UIView+GCLibrary.h" @implementation UIView (GCLibrary) - (CGFloat) height { return self.frame.size.height; } - (CGFloat) width { return self.frame.size.width; } - (CGFloat) x { return self.frame.origin.x; } - (CGFloat) y { return self.frame.origin.y; } - (CGFloat) centerY { return self.center.y; } - (CGFloat) centerX { return self.center.x; } - (void) setHeight:(CGFloat) newHeight { CGRect frame = self.frame; frame.size.height = newHeight; self.frame = frame; } - (void) setWidth:(CGFloat) newWidth { CGRect frame = self.frame; frame.size.width = newWidth; self.frame = frame; } - (void) setX:(CGFloat) newX { CGRect frame = self.frame; frame.origin.x = newX; self.frame = frame; } - (void) setY:(CGFloat) newY { CGRect frame = self.frame; frame.origin.y = newY; self.frame = frame; } @end 
+32
Mar 01 2018-11-11T00:
source share

UIView also has a center property. If you just want to move the position, rather than resize it, you can just change it - for example:

aView.center = CGPointMake(50, 200);

Otherwise, you will do it the way you posted.

+12
Mar 01 '11 at 10:23
source share

I found a similar approach (it also uses a category) with gcamp's answer, which helped me significantly here . In your case, this is simple:

 aView.topLeft = CGPointMake(100, 200); 

but if you want, for example, horizontally and to the left with a different view, you can simply:

 aView.topLeft = anotherView.middleLeft; 
+6
Jan 25 '13 at 17:39
source share

The solution in the selected answer does not work if Autolayout is used. If you use Autolayout for submissions, take a look at this answer .

+6
Nov 15 '13 at 11:50
source share

Since then, CGRectOffset been replaced by the offsetBy instance offsetBy .

https://developer.apple.com/reference/coregraphics/cgrect/1454841-offsetby

For example, what used to be

 aView.frame = CGRectOffset(aView.frame, 10, 10) 

now will be

 aView.frame = aView.frame.offsetBy(dx: CGFloat(10), dy: CGFloat(10)) 
+5
Apr 12 '17 at 22:15
source share
 aView.frame = CGRectMake(100, 200, aView.frame.size.width, aView.frame.size.height); 
+4
Mar 01 '11 at 10:24
source share

In my work we do not use macros. So the solution provided by @TomSwift inspired me. I see an implementation for CGRectMake and create the same CGRectSetPos, but without macros.

 CG_INLINE CGRect CGRectSetPos(CGRect frame, CGFloat x, CGFloat y) { CGRect rect; rect.origin.x = x; rect.origin.y = y; rect.size.width = frame.size.width; rect.size.height = frame.size.height; return rect; } 

To use only frame, X and Y

 viewcontroller.view.frame = CGRectSetPos(viewcontroller.view.frame, 100, 100); 

Work with me ^ _ ^

+2
Oct 30 '13 at 21:12
source share

If someone needs the light Swift extension to easily change UIView fields - you can use this

 view.top = 16 view.right = self.width view.bottom = self.height self.height = view.bottom 
+2
Nov 28 '15 at 10:17
source share

@TomSwift Swift 3 answer

 aView.center = CGPoint(x: 150, y: 150); // set center 

or

 aView.frame = CGRect(x: 100, y: 200, width: aView.frame.size.width, height: aView.frame.size.height ); // set new position exactly 

or

 aView.frame = aView.frame.offsetBy(dx: CGFloat(10), dy: CGFloat(10)) // offset by an amount 
+1
Oct 23 '17 at 10:08 on
source share

Here is Swift 3's answer for those who watch, since Swift 3 does not accept "Make."

 aView.center = CGPoint(x: 200, Y: 200) 
0
Jan 09 '17 at 6:16
source share

Another way:

 CGPoint position = CGPointMake(100,30); [self setFrame:(CGRect){ .origin = position, .size = self.frame.size }]; 

This I save the size settings and change only the beginning.

0
Sep 17 '17 at 2:31 on
source share



All Articles