iOS Development-uiview Extension CGRect

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.