[ios] Simple way to change the position of 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?

This question is related to ios layout uiview frame cgrect

The answer is


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

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


swift

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

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.


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

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.


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.


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

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;

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

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

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

@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

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

Examples related to ios

Adding a UISegmentedControl to UITableView Crop image to specified size and picture location Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Autoresize View When SubViews are Added Warp \ bend effect on a UIView? Speech input for visually impaired users without the need to tap the screen make UITableViewCell selectable only while editing Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

Examples related to layout

This view is not constrained What's the difference between align-content and align-items? CSS Flex Box Layout: full-width row and columns Fill remaining vertical space with CSS using display:flex What is setContentView(R.layout.main)? How to change line color in EditText Scrolling a flexbox with overflowing content UICollectionView - Horizontal scroll, horizontal layout? How to style a div to be a responsive square? 100% width Twitter Bootstrap 3 template

Examples related to uiview

How can I mimic the bottom sheet from the Maps app? UIView touch event in controller Swift addsubview and remove it Swift UIView background color opacity How do I change UIView Size? How to add constraints programmatically using Swift Load a UIView from nib in Swift Remove all constraints affecting a UIView How do I write a custom init for a UIView subclass in Swift? How to use UIVisualEffectView to Blur Image?

Examples related to frame

Adjust UILabel height to text What is the height of Navigation Bar in iOS 7? How do I change the background of a Frame in Tkinter? Tkinter scrollbar for frame Python Tkinter clearing a frame Switch between two frames in tkinter Html code as IFRAME source rather than a URL UIView frame, bounds and center Simple way to change the position of UIView? What's the difference between IFrame and Frame?

Examples related to cgrect

IOS: verify if a point is inside a rect Simple way to change the position of UIView?