About UIView location will encounter, generally need to change the position of UIView, need to get the original frame position, and then modify the frame above, sometimes if just change the vertical direction of the position, width and height of one, this writing is very troublesome. The second of the following two formulations is significantly simpler, and if you need to implement the second approach you need to extend UIView.
1 cgrect frame=self.testview.frame; frame.size.width=120; Self.testview.frame=frame; [Self printframe]; 2 self.testview.width=120; [Self printframe];
Extension definition:
@interface UIView (ReSize) @property (nonatomic, assign) cgsize size; @property (nonatomic,assign) cgfloat x;@ Property (nonatomic,assign) cgfloat y; @property (nonatomic, assign) cgfloat top; @property (nonatomic, assign) CGFloat bottom; @property (nonatomic, assign) CGFloat left, @property (nonatomic, assign) CGFloat right; @property ( Nonatomic, assign) CGFloat CenterX, @property (nonatomic, assign) CGFloat centery; @property (nonatomic, assign) CGFloat WI DTH; @property (nonatomic, assign) cgfloat height; @end
Extension implementations:
@implementation UIView (ReSize)-(cgsize) size; {return [self frame].size;} -(void) SetSize: (cgsize) size; {Cgpoint origin = [self frame].origin; [Self Setframe:cgrectmake (origin.x, ORIGIN.Y, Size.width, Size.Height)];} -(cgfloat) x{cgrect frame=[self frame]; return frame.origin.x;} -(void) SetX: (cgfloat) x{cgrect frame=[self frame]; Frame.origin.x=x; [Self setframe:frame];} -(cgfloat) y{cgrect frame=[self frame]; return FRAME.ORIGIN.Y;} -(void) Sety: (cgfloat) y{cgrect frame=[self frame]; Frame.origin.y=y; return [self setframe:frame];} -(cgfloat) left; {return Cgrectgetminx ([self frame]);} -(void) Setleft: (cgfloat) x; {CGRect frame = [self frame]; frame.origin.x = x; [Self setframe:frame];} -(cgfloat) top; {return Cgrectgetminy ([self frame]);} -(void) Settop: (cgfloat) y; {CGRect frame = [self frame]; FRAME.ORIGIN.Y = y; [Self setframe:frame];} -(CGFloat) right; {return Cgrectgetmaxx ([self frame]);} -(void) Setright: (CGFloat) right; {CGrect frame = [self frame]; frame.origin.x = Right-frame.size.width; [Self setframe:frame];} -(cgfloat) bottom; {return Cgrectgetmaxy ([self frame]);} -(void) Setbottom: (cgfloat) Bottom; {CGRect frame = [self frame]; FRAME.ORIGIN.Y = Bottom-frame.size.height; [Self setframe:frame];} -(cgfloat) CenterX; {return [self center].x;} -(void) Setcenterx: (cgfloat) CenterX; {[Self Setcenter:cgpointmake (CenterX, Self.center.y)];} -(cgfloat) centery; {return [self center].y;} -(void) Setcentery: (cgfloat) CenterY; {[Self Setcenter:cgpointmake (self.center.x, CenterY)];} -(cgfloat) width; {return Cgrectgetwidth ([self frame]);} -(void) SetWidth: (cgfloat) width; {CGRect frame = [self frame]; Frame.size.width = width; [Self setframe:cgrectstandardize (frame)];} -(cgfloat) height; {return Cgrectgetheight ([self frame]);} -(void) SetHeight: (cgfloat) height; {CGRect frame=[self frame]; Frame.size.height = height; [Self setframe:cgrectstandardize (frame)];} @end
Project source code address: Https://github.com/SmallElephant/iOS-FEViewReSize
iOS Development-uiview Extension CGRect