Without using xib, the process of pure code development, the dynamic UI needs to change the size of the object repeatedly using CGRectMake to setframe very inefficient time-consuming, and lead a whole body.
Here are a few ways to facilitate dynamic layout
1.set method re-layout
#import <Foundation/Foundation.h>@interface huofarutils:nsobject@endcgrect cgrectsetwidth (cgrect rect, cgfloat width); CGRect cgrectsetheight (cgrect rect, cgfloat height); CGRect cgrectsetsize (cgrect rect, cgsize size); CGRect cgrectsetx (CGRect rect, cgfloat x); CGRect cgrectsety (CGRect rect, cgfloat y); CGRect Cgrectsetorigin (CGRect rect, Cgpoint origin); #import"HuofarUtils.h"@implementation huofarutils@endcgrect cgrectsetwidth (cgrect rect, cgfloat width) {returnCGRectMake (rect.origin.x, RECT.ORIGIN.Y, Width, rect.size.height);} CGRect cgrectsetheight (cgrect rect, cgfloat height) {returnCGRectMake (rect.origin.x, RECT.ORIGIN.Y, rect.size.width, height);} CGRect cgrectsetsize (cgrect rect, cgsize size) {returnCGRectMake (rect.origin.x, RECT.ORIGIN.Y, Size.width, size.height);} CGRect cgrectsetx (CGRect rect, cgfloat x) {returnCGRectMake (x, RECT.ORIGIN.Y, Rect.size.width, rect.size.height);} CGRect cgrectsety (CGRect rect, cgfloat y) {returnCGRectMake (rect.origin.x, y, Rect.size.width, rect.size.height);} CGRect Cgrectsetorigin (CGRect rect, Cgpoint origin) {returnCGRectMake (origin.x, ORIGIN.Y, Rect.size.width, rect.size.height);}
2. Get method gets
cgrectgetheight/width/x/Y System comes with
3. Take Uilabel For example get dynamic height or width
(1) Width fixed get lab dynamic height
-(CGFloat) Heightfortitle: (NSString *) title{//momentwith Static WidthCGRect Tmprect = [titleBoundingrectwithsize:cgsizemake (Momentwidth, -) Options:nsstringdrawinguseslinefragmentorigin //Optional calculation reference conditions Attributes:[nsdictionary Dictionarywithobjectsandkeys:[Uifont Boldsystemfontof Size: - ], Nsfontattributename, Nil] context:nil]; returntmpRect.size.height;}
(2) Highly fixed get lab dynamic width
-(CGFloat) Widthfortitle: (NSString *) title{ *attribute = @{nsfontattributename:[Uifont Systemfontofsize:+]}; = [title] | Nsstringdrawinguseslinefragmentorigin | nsstringdrawingusesfontleading attributes:attribute context:nil].size; return size.width;}
The above two methods are pretty much the same. The other is the dynamically obtained value
IOS UI Frame relative position adjustment and optimization method Height Width X Y adjustment