The Frame property of view in iOS is used too frequently, especially when adjusting the UI. We know that under normal circumstances we cannot modify a property of frame (x,y,width,height, etc.) individually, for example: someview.frame.x = 100; This way is not allowed, But what we've actually encountered more often is that most of the element values of frame remain the same, changing only part of it. Believing this annoyance has troubled many people, so we have to use the following two ways to achieve the purpose: 1:cgrect frame = someview.frame;frame.x =100;frame.width = 200;someview.frame = Frame FA 2:someview.frame = CGRectMake (x, xxx, x, XXX); Law 2 seems to be very streamlined, but it is also cumbersome, because the actual application scenario in which the value of the four values are dependent on the other variables, y, width, height The statement leading to 2 is very long. In short, the above methods are not "elegant". How does it count as elegance? I think it would be perfect if we could modify a value directly as follows: someview.x = 100;someview.width = 200; We skipped the frame property of Someview and directly modified the value of the element we wanted. Fortunately, we use category can be quite convenient to achieve the purpose, this is a once and for all, the introduction of a category after the entire project can use this modification method: Uiview+frame.hwzlcodelibrarycreated by Wzl On 15/3/23.copyright (c) 2015 Weng-zilin. All rights reserved. #import <UIKit/UIKit.h> @interface UIView (Frame) @property (nonatomic, assign) CGFloat x; @prop Erty (nonatomic, assign) cgfloat y; @property (nonatomic, assign) cgfloat width; @property (nonatomic, assign) CGFloat Heigh t; @property (nonatomic, assign) Cgpoint origin; @property (nonatomic, Assign) cgsize size; @end uiview+frame.mwzlcodelibrary#import "Uiview+frame.h" @implementation UIView (Frame)-(void) SetX: ( CGFloat) x{CGRect frame = self.frame; frame.origin.x = x; Self.frame = frame; }-(CGFloat) x {return self.frame.origin.x;}-(void) Sety: (cgfloat) y {cgrect frame = self.frame; FRAME.ORIGIN.Y = y; Self.frame = frame; }-(CGFloat) y {return self.frame.origin.y;}-(void) Setorigin: (Cgpoint) origin {CGRect frame = self.frame; Frame.origin = origin; Self.frame = frame; }-(Cgpoint) origin {return self.frame.origin;}-(void) SetWidth: (cgfloat) Width {cgrect frame = self.frame; Frame.size.width = width; Self.frame = frame; }-(CGFloat) width {return self.frame.size.width;}-(void) SetHeight: (cgfloat) Height {cgrect frame = Self.frame ; Frame.size.height = height; Self.frame = frame; }-(CGFloat) height {return self.frame.size.height;}-(void) SetSize: (cgsize) Size {CGRect frame = Self.fraMe frame.size = size; Self.frame = frame; }-(Cgsize) size {return self.frame.size;} @end
An easy way to set up a frame on iOS