We modify a value in the frame, which requires tedious writing, for example:
(1). Directly set the position size
view.frame = CGRectMake(0, 0, 320, 150);
(2). Modify only a value
100, view.frame.size.width, view.frame.size.height);
This kind of writing in the interface element more layout, will make the programmer very painful, and the readability will weaken
If can be humanized a little, you can modify a value individually, or a little more humane, you can output the coordinates of each point in the view, will be more conducive to layout. We can achieve this humanized layout operation by creating the category of view.
I. Establishment of CATEGORIES
To create a uiview category named layout, the code for the category is as follows:
1. Uiview+layout.h
The. h file defines the easy-to-read properties of x, y, width, height, and the code is as follows:
#import<UIKit/UIKit.h>@interfaceUIView (Layout)@property (Assignnonatomic)CGFloat top;@property (Assignnonatomic)CGFloat Bottom;@property (Assignnonatomic)CGFloat left;@property (Assignnonatomic)CGFloat right;@property (Assignnonatomic)CGFloat x;@property (Assignnonatomic)CGFloat y;@property (Assignnonatomic)cgpoint origin; @property (assign, nonatomic) Span class= "hljs-built_in" >cgfloat CenterX; @property (assign, nonatomic) Span class= "hljs-built_in" >cgfloat centery; @property (assign, nonatomic) Span class= "hljs-built_in" >cgfloat width; @property (assign, nonatomic) Span class= "hljs-built_in" >cgfloat height; @property (assign, nonatomic) Span class= "hljs-built_in" >cgsize size; @end
2. uiview+layout.m
The. m file implements the setter and getter methods for each property, with the following code:
#import"Uiview+layout.h"@implementationUIView (Layout)@dynamic top;@dynamic Bottom;@dynamic left;@dynamic right;@dynamic width;@dynamic height;@dynamic size;@dynamic x;@dynamic y;-(CGFloat) top{ReturnSelf. Frame. Origin. Y;} - (void) Settop: (CGFloat) top{CGRect frame =Self. Frame; Frame. Origin. y = top;Self. frame = frame;} - (CGFloat) left{ReturnSelf. Frame. Origin. x;} - (void) Setleft: (CGFloat) left{CGRect frame =Self. Frame; Frame. Origin. x = left;Self. frame = frame;} - (CGFloat) bottom{ReturnSelf. Frame. Size. Height +Self. Frame. Origin. Y;} - (void) Setbottom: (CGFloat) bottom{CGRect frame =Self. Frame; Frame. Origin. y = Bottom-frame. Size. Height;Self. frame = frame;} - (CGFloat) right{ReturnSelf. Frame. Size. Width +Self. Frame. Origin. x;} - (void) Setright: (CGFloat) right{CGRect frame =Self. Frame; Frame. Origin. x = Right-frame. Size. width;Self. frame = frame;} - (CGFloat) x{ReturnSelf. Frame. Origin. x;} - (void) SetX: (CGFloat) value{CGRect frame =Self. Frame; Frame. Origin. x = value;Self. frame = frame;} - (CGFloat) y{ReturnSelf. Frame. Origin. Y;} - (void) Sety: (CGFloat) value{CGRect frame =Self. Frame; Frame. Origin. y = value;Self. frame = frame;} - (Cgpoint) origin{ReturnSelf. Frame. Origin;} - (void) Setorigin: (Cgpoint) origin{CGRect frame =Self. Frame; Frame. origin = origin;Self. frame = frame;} - (CGFloat) centerx{ReturnSelf. Center. x;} - (void) Setcenterx: (CGFloat) centerx{Cgpoint Center =Self. Center; Center. x = CenterX;Self. Center = Center;} - (CGFloat) centery{ReturnSelf. Center. Y;} - (void) Setcentery: (CGFloat) centery{Cgpoint Center =Self. Center; Center. y = centery;Self. Center = Center;} - (CGFloat) width{ReturnSelf. Frame. Size. Width;} - (void) SetWidth: (CGFloat) width{CGRect frame =Self. Frame; Frame. Size. width = width;Self. frame = frame;} - (CGFloat) height{ReturnSelf. Frame. Size. Height;} - (void) SetHeight: (cgfloat) height{cgrect frame = Self.frame; Frame.sizeself.frame = frame;} -(cgsize) size{return self< Span class= "hljs-variable" >.frame.size;} -(void) SetSize: (cgsize) size{ cgrect frame = self.frame; Frame.size = size; self.frame = frame;} @end
Second, use
After the category is created, the layout of the view is much easier, such as when you want to modify the y-coordinate:
100, view.frame.size.width, view.frame.size.height);
And now it just needs to be written like this:
100;
Both concise and convenient
IOS UIView quickly Modify frame