Simple way to change the position of UIView?

IosLayoutUiviewFrameCgrect

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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 );

Solution 2 - Ios

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

.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

Solution 3 - Ios

UIView's also have a center property. If you just want to move the position rather than resize, you can just change that - eg:

aView.center = CGPointMake(50, 200);

Otherwise you would do it the way you posted.

Solution 4 - Ios

I found a similar approach (it uses a category as well) with gcamp's answer that helped me greatly here. In your case is as simple as this:

aView.topLeft = CGPointMake(100, 200);

but if you want for example to centre horizontal and to the left with another view you can simply:

aView.topLeft = anotherView.middleLeft;

Solution 5 - Ios

The solution in the selected answer does not work in case of using Autolayout. If you are using Autolayout for views take a look at this answer.

Solution 6 - Ios

CGRectOffset has since been replaced with the instance method offsetBy.

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

For example, what used to be

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

would now be

aView.frame = aView.frame.offsetBy(dx: CGFloat(10), dy: CGFloat(10))

Solution 7 - Ios

aView.frame = CGRectMake(100, 200, aView.frame.size.width, aView.frame.size.height);

Solution 8 - Ios

In my work we not use macros. So the solution provide by @TomSwift inspired to me. I see the 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 I only put frame, X and Y

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

Work for me ^_^

Solution 9 - Ios

If anybody needs light Swift extension to change UIView margins easily - you can use this

view.top = 16
view.right = self.width
view.bottom = self.height
self.height = view.bottom

Solution 10 - Ios

@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

Solution 11 - Ios

Here is the Swift 3 answer for anyone looking since Swift 3 does not accept "Make".

aView.center = CGPoint(x: 200, Y: 200)

Solution 12 - Ios

Other way:

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

This i save size parameters and change origin only.

Solution 13 - Ios

swift

view.frame = view.frame.offsetBy(dx: offsetX, dy: offsetY)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSeunghoonView Question on Stackoverflow
Solution 1 - IosTomSwiftView Answer on Stackoverflow
Solution 2 - IosgcampView Answer on Stackoverflow
Solution 3 - IoslxtView Answer on Stackoverflow
Solution 4 - IosMedaView Answer on Stackoverflow
Solution 5 - IosSahilView Answer on Stackoverflow
Solution 6 - IosRoopeView Answer on Stackoverflow
Solution 7 - IosMark AdamsView Answer on Stackoverflow
Solution 8 - IosgzfranciscoView Answer on Stackoverflow
Solution 9 - Ioskatleta3000View Answer on Stackoverflow
Solution 10 - IosAyman IbrahimView Answer on Stackoverflow
Solution 11 - IosPearson BashamView Answer on Stackoverflow
Solution 12 - IosBimawaView Answer on Stackoverflow
Solution 13 - IosWilliam HuView Answer on Stackoverflow