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
阅读全文
0 0
- iOS 工具类-----frame的设置和自适应
- iOS 动态设置UILabel的frame(宽度和高度)
- 文字自适应label的frame
- iOS 自定义工具类 ---- 自适应输入框
- iOS 中frame 和 bounds 的区别
- ios视图frame和bounds的对比
- iOS中frame和bounds的区别
- iOS Frame和bounds的区别
- ios视图frame和bounds的对比
- iOS View frame和bounds的区别
- iOS---frame和bounds的区别
- iOS,frame和bounds的区别
- iOS中frame和bounds的区别
- ios视图frame和bounds的对比
- iOS中frame和bounds的区别
- ios bounds 和 frame
- ios frame和bounds
- frame 和 bounds IOS
- 块级元素和行内元素多种——水平,垂直、水平垂直居中的方法
- 文本挖掘预处理之向量化与Hash Trick
- 旅游类App原型制作分享-Triposo
- codeforces 864E Fire (排序+背包dp)
- 【oracle】如何将dmp文件导入到自己的oracle数据库中
- iOS 工具类-----frame的设置和自适应
- 一键安装 python2.7.12 & python3.6.0 &pip 至 centos
- VC与JavaScript交互(二) ———— 调用JS函数
- 《C++ Primer Plus(第6版)》编程练习代码 Chapter 4
- javaScript鼠标事件实现图片转换和换肤
- call 和apply用法
- kaggle-泰塔尼克号-GBDT-快速分析
- 2017北京网络赛 Hihocoder 1580 Matrix DP
- springboot整合Quartz实现动态配置定时任务