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?
Frame A view's frame ( CGRect ) is the position of its rectangle in the superview 's coordinate system. By default it starts at the top left. Bounds A view's bounds ( CGRect ) expresses a view rectangle in its own coordinate system.
The UIView class is a concrete class that you can instantiate and use to display a fixed background color. You can also subclass it to draw more sophisticated content.
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 );
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With