iOS 工具类-----frame的设置和自适应

来源:互联网 发布:gmip源码分析 编辑:程序博客网 时间:2024/05/17 19:15

新建UIView的分类:AdjustFrame

.h

#import <UIKit/UIKit.h>#define kAdjust(a) [UIScreen mainScreen].bounds.size.width/375.0*a@interface UIView (AdjustFrame)/*---------------------Frame---------------------*//** View起始位置 */@property (nonatomic) CGPoint viewOrigin;/** View尺寸 */@property (nonatomic) CGSize viewSize;/*---------------------Frame Origin---------------------*//** origin.x */@property (nonatomic) CGFloat x;/** origin.y */@property (nonatomic) CGFloat y;/*---------------------Frame Size---------------------*//** size.width */@property (nonatomic) CGFloat width;/** size.height */@property (nonatomic) CGFloat height;/*---------------------Frame Borders---------------------*//** 顶部 */@property (nonatomic) CGFloat top;/** 左边 */@property (nonatomic) CGFloat left;/** 底部 */@property (nonatomic) CGFloat bottom;/** 右边 */@property (nonatomic) CGFloat right;/*---------------------Center Point---------------------*//** center.x */@property (nonatomic) CGFloat centerX;/** center.y */@property (nonatomic) CGFloat centerY;/*---------------------Middle Point---------------------*//** 本身坐标的中心点 */@property (nonatomic, readonly) CGPoint middlePoint;/** 本身坐标的中心x */@property (nonatomic, readonly) CGFloat middleX;/** 本身坐标的y */@property (nonatomic, readonly) CGFloat middleY;/** frame适配(根据屏幕比) */-(void)adjustFrame;/** 适配所有的view的宽,高度不适配(根据屏幕比) */-(void)adjustWidthAllViews;/** 根据父视图实现子视图适配 @param scaleX x的伸缩比率 @param scaleY y的伸缩比率 @param scaleW w的伸缩比率 @param scaleH h的伸缩比率 */-(void)adjustFrameWithXScale:(CGFloat)scaleX YScale:(CGFloat)scaleY WScale:(CGFloat)scaleW HScale:(CGFloat)scaleH;@end

.m

#import "UIView+AdjustFrame.h"@implementation UIView (AdjustFrame)#pragma mark Frame-(CGPoint)viewOrigin{    return self.frame.origin;}-(void)setViewOrigin:(CGPoint)viewOrigin{    CGRect newFrame = self.frame;    newFrame.origin = viewOrigin;    self.frame = newFrame;}-(CGSize)viewSize{    return self.frame.size;}-(void)setViewSize:(CGSize)viewSize{    CGRect newFrame = self.frame;    newFrame.size = viewSize;    self.frame = newFrame;}#pragma mark Frame Origin-(CGFloat)x{    return self.frame.origin.x;}-(void)setX:(CGFloat)x{    CGRect newFrame = self.frame;    newFrame.origin.x = x;    self.frame = newFrame;}-(CGFloat)y{    return self.frame.origin.y;}-(void)setY:(CGFloat)y{    CGRect newFrame = self.frame;    newFrame.origin.y = y;    self.frame = newFrame;}#pragma mark Frame Size- (CGFloat)height{    return self.frame.size.height;}-(void)setHeight:(CGFloat)newHeight{    CGRect newFrame = self.frame;    newFrame.size.height = newHeight;    self.frame = newFrame;}- (CGFloat)width{    return self.frame.size.width;}- (void)setWidth:(CGFloat)newWidth{    CGRect newFrame = self.frame;    newFrame.size.width = newWidth;    self.frame = newFrame;}#pragma mark Frame Borders-(CGFloat)left{    return self.x;}-(void)setLeft:(CGFloat)left{    self.x = left;}-(CGFloat)right{    return self.x + self.width;}-(void)setRight:(CGFloat)right{    self.x = right - self.width;}-(CGFloat)top{    return self.y;}-(void)setTop:(CGFloat)top{    self.y = top;}-(CGFloat)bottom{    return self.y + self.height;}-(void)setBottom:(CGFloat)bottom{    self.y = bottom - self.height;}#pragma mark Center Point-(CGFloat)centerX{    return self.center.x;}-(void)setCenterX:(CGFloat)newCenterX{    self.center = CGPointMake(newCenterX, self.centerY);}-(CGFloat)centerY{    return self.center.y;}-(void)setCenterY:(CGFloat)newCenterY{    self.center = CGPointMake(self.centerX, newCenterY);}#pragma mark Middle Point- (CGPoint)middlePoint{    return CGPointMake(self.middleX, self.middleY);}- (CGFloat)middleX{    return self.width / 2;}- (CGFloat)middleY{    return self.height / 2;}#pragma mark AdjustFrame// !!!: 调整所有视图的frame-(void)adjustFrame{    self.frame = CGRectMake(kAdjust(self.x), kAdjust(self.y), kAdjust(self.width), kAdjust(self.height));    [self adjustSubViewsFrameWithView:self];}-(void)adjustSubViewsFrameWithView:(UIView*)view{    [view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {        obj.frame = CGRectMake(kAdjust(obj.x), kAdjust(obj.y), kAdjust(obj.width), kAdjust(obj.height));        [self adjustSubViewsFrameWithView:obj];    }];}// !!!:  调整视图的width-(void)adjustWidthAllViews{    self.x      = kAdjust(self.x);    self.width  = kAdjust(self.width);    [self adjustSubViewsWithSuperView:self];}-(void)adjustSubViewsWithSuperView:(UIView*)view{    [view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {        obj.x      = kAdjust(self.x);        obj.width  = kAdjust(self.width);        [self adjustSubViewsWithSuperView:obj];    }];}-(void)adjustFrameWithXScale:(CGFloat)scaleX YScale:(CGFloat)scaleY WScale:(CGFloat)scaleW HScale:(CGFloat)scaleH{    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {        obj.frame = CGRectMake(obj.x*scaleX, obj.y*scaleY, obj.width*scaleW, obj.height*scaleH);    }];}@end