Simple way to change the position of UIView?
Asked Answered
C

13

73

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?

Crush answered 1/3, 2011 at 22:18 Comment(1)
Note: It doesn't work if you are using AutoLayouts.Ifill
A
232
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 );
Alurta answered 1/3, 2011 at 22:26 Comment(3)
Yay, upvote from me as well for CGRectOffset. It cuts down on the gibberish. Gibberish I now get to spend on something else.Enkindle
For Swift 3: aView.frame = aView.frame.offsetBy(dx: 100, dy: 100)Bamboo
Is there a way to animate this transition - it looks rather jarring.Fairtrade
I
33

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
Interpretive answered 1/3, 2011 at 22:42 Comment(2)
I ended up making the the exact same class methods but didn't think of appending it to the original UIView class! +1 for being practical and clever!Photodisintegration
If you need something like this in Swift - feel free to use github.com/katleta3000/UIView-FrameBauble
F
13

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.

Finochio answered 1/3, 2011 at 22:23 Comment(0)
A
7

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;
Austronesian answered 25/1, 2013 at 17:39 Comment(0)
H
6

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.

Hubblebubble answered 15/11, 2013 at 11:50 Comment(0)
L
6

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))
Lm answered 12/4, 2017 at 22:15 Comment(0)
J
4
aView.frame = CGRectMake(100, 200, aView.frame.size.width, aView.frame.size.height);
Jodoin answered 1/3, 2011 at 22:24 Comment(2)
i think we were typing the same thing all at the same time.Alurta
I love the thrill of trying to have the best answer first.Jodoin
C
2

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 ^_^

Cameral answered 30/10, 2013 at 21:12 Comment(1)
I check the original implementation of CGRectMake and this is the way they (Apple) do it. So I follow them.Cameral
B
2

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
Bauble answered 28/11, 2015 at 10:17 Comment(0)
C
2

@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
Conveyance answered 23/10, 2017 at 10:8 Comment(0)
I
1

swift

view.frame = view.frame.offsetBy(dx: offsetX, dy: offsetY)
Ipecac answered 26/5, 2020 at 2:56 Comment(0)
F
0

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

aView.center = CGPoint(x: 200, Y: 200)
Fda answered 9/1, 2017 at 6:16 Comment(0)
C
0

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.

Crossbeam answered 17/9, 2017 at 14:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.