ExtUIView.h
@interface UIView (autoresizing) //带margin的左中右层 - (void) addSubviewLeft:(UIView *)leftView middleView:(UIView *)middleView rightView:(UIView *)rightView lefWidth:(CGFloat)lefWidth rightWidth:(CGFloat)rightWidth margin:(CGRect)marginFrame; //左中右层 - (void) addSubviewLeft:(UIView *)leftView middleView:(UIView *)middleView rightView:(UIView *)rightView lefWidth:(CGFloat)lefWidth rightWidth:(CGFloat)rightWidth; //上中下层 - (void) addSubviewTop:(UIView *)topView middleView:(UIView *)middleView bottomView:(UIView *)bottomView topHeight:(CGFloat)topHeight bottomHeight:(CGFloat)bottomHeight; //左上右下 自适应大小来满足margin - (void) addSubview:(UIView *)aView margin:(CGRect)marginFrame; //自适应margin来满足大小 - (void) addSubview:(UIView *)aView size:(CGSize)aSize; //左上右下 自适应大小来满足margin - (void) setSizeInView:(UIView *)aView margin:(CGRect)marginFrame; //左上右下 自适应margin来满足大小 - (void) setCenterInView:(UIView *)aView size:(CGSize)aSize; - (CGPoint) centerPoint; @end
ExtUIView.m
#import "ExtUIView.h" @implementation UIView (autoresizing) - (void) addSubviewLeft:(UIView *)leftView middleView:(UIView *)middleView rightView:(UIView *)rightView lefWidth:(CGFloat)lefWidth rightWidth:(CGFloat)rightWidth margin:(CGRect)marginFrame { ; } - (void) addSubviewLeft:(UIView *)leftView middleView:(UIView *)middleView rightView:(UIView *)rightView lefWidth:(CGFloat)lefWidth rightWidth:(CGFloat)rightWidth { if(leftView) { leftView.frame = CGRectMake(0, 0, lefWidth, self.frame.size.height); leftView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin; [self addSubview:leftView]; } if(middleView) { middleView.frame = CGRectMake(lefWidth, 0, self.frame.size.width - lefWidth - rightWidth, self.frame.size.height); middleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [self addSubview:middleView]; } if(rightView) { rightView.frame = CGRectMake(self.frame.size.width - rightWidth, 0, rightWidth, self.frame.size.height); rightView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin ; [self addSubview:rightView]; } } - (void) addSubviewTop:(UIView *)topView middleView:(UIView *)middleView bottomView:(UIView *)bottomView topHeight:(CGFloat)topHeight bottomHeight:(CGFloat)bottomHeight { if(topView) { topView.frame = CGRectMake(0, 0, self.frame.size.width, topHeight); topView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin; [self addSubview:topView]; } if(middleView) { middleView.frame = CGRectMake(0, topHeight, self.bounds.size.width, self.frame.size.height - topHeight - bottomHeight); middleView.autoresizingMask =UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth ; [self addSubview:middleView]; } if(bottomView) { bottomView.frame = CGRectMake(0, self.frame.size.height - bottomHeight, self.frame.size.width, bottomHeight); bottomView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin ; [self addSubview:bottomView]; } } //左上右下 - (void) addSubview:(UIView *)aView margin:(CGRect)marginFrame { if (!aView) return; aView.frame = CGRectMake(marginFrame.origin.x, marginFrame.origin.y, self.frame.size.width - marginFrame.origin.x - marginFrame.size.width, self.frame.size.height - marginFrame.origin.y - marginFrame.size.height); aView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [self addSubview:aView]; } - (void) addSubview:(UIView *)aView size:(CGSize)aSize { if (!aView) return; aView.center = [self centerPoint]; aView.bounds = CGRectMake(0, 0, aSize.width, aSize.height); aView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; [self addSubview:aView]; } //左上右下 - (void) setSizeInView:(UIView *)aView margin:(CGRect)marginFrame { if (!aView) return; self.frame = CGRectMake(marginFrame.origin.x, marginFrame.origin.y, aView.frame.size.width - marginFrame.origin.x - marginFrame.size.width, aView.frame.size.height - marginFrame.origin.y - marginFrame.size.height); self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; } - (void) setCenterInView:(UIView *)aView size:(CGSize)aSize { if (!aView) return; self.center = [aView centerPoint]; self.bounds = CGRectMake(0, 0, aSize.width, aSize.height); self.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleBottomMargin; } - (CGPoint) centerPoint { return CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2); } @end
示例:
UIView *v1 = [[[UIView alloc] init] autorelease]; UIView *v2 = [[[UIView alloc] init] autorelease]; UIView *v3 = [[[UIView alloc] init] autorelease]; UIView *v4 = [[[UIView alloc] init] autorelease]; UIView *v5 = [[[UIView alloc] init] autorelease]; UIView *v6 = [[[UIView alloc] init] autorelease]; UIView *v7 = [[[UIView alloc] init] autorelease]; UIView *v8 = [[[UIView alloc] init] autorelease]; UIView *v9 = [[[UIView alloc] init] autorelease]; UIView *v10 = [[[UIView alloc] init] autorelease]; UIView *v11 = [[[UIView alloc] init] autorelease]; UIView *v12 = [[[UIView alloc] init] autorelease]; v1.backgroundColor=[UIColor cyanColor]; v2.backgroundColor=[UIColor grayColor]; v3.backgroundColor=[UIColor greenColor]; v4.backgroundColor=[UIColor greenColor]; v5.backgroundColor=[UIColor blackColor]; v6.backgroundColor=[UIColor cyanColor]; v7.backgroundColor=[UIColor darkGrayColor]; v8.backgroundColor=[UIColor lightGrayColor]; v9.backgroundColor=[UIColor whiteColor]; v10.backgroundColor=[UIColor blackColor]; v11.backgroundColor=[UIColor orangeColor]; v12.backgroundColor=[UIColor purpleColor]; [self.view addSubviewLeft:v1 middleView:v2 rightView:v3 lefWidth:100.0 rightWidth:100.0]; [v1 addSubviewTop:v7 middleView:v8 bottomView:v9 topHeight:100 bottomHeight:50]; [v8 addSubview:v4 size:CGSizeMake(15, 15)]; [v2 addSubview:v5 size:CGSizeMake(55, 55)]; [v3 addSubview:v6 size:CGSizeMake(15, 15)]; [v9 addSubview:v10 margin:CGRectMake(10, 10, 10, 10)]; [v11 setCenterInView:v2 size:CGSizeMake(50, 50)]; [v12 setSizeInView:v2 margin:CGRectMake(50, 10, 50, 100)]; [v2 addSubview:v11]; [v2 addSubview:v12];
示例图:
相关推荐
拖到工程就能用,纯代码屏幕适配
基于UIView扩展的类目,用于设置上下左右圆角,可设置单边圆角,也可设置为圆形 github地址:https://github.com/LQQZYY/UIViewCornerDemo 喜欢的话就给颗星支持一下,感谢!
UIView+Extension 对view的一个扩展
为UIView添加圆角扩展
封装了oc和 swift的 2个UIView的虚线外框的类扩展,可以设置线的长度,间隙长度,虚线宽度,虚线颜色,外框圆角边框,一行代码调用非常方便
主要介绍了IOS 开发之swift中UIView的扩展使用的实例的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
通过协议扩展高斯模糊任意 UIView.zip,对任何具有swift协议扩展的uiview应用高斯模糊
##UIView扩展 ####直接获取view的方位属性 view.height view.width ####针对不同设备的坐标转换 define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width define SCREENPOINTRESIZE(float) ((float)/320.0...
需要什么版本自己拖到项目,两者只需其中一个 Swift只需要拖D3View.swift到项目,Objective-c需要D3View.h和D3View.m到项目,都是UIView扩展类 因为是扩展,所以只要是uiview的子类都可以使用。 动画都是以d3_开头的...
扩展UIView的下拉菜单。.zip,uiview中的下拉菜单(按扩展名)
iOS中对UIView类的扩展,可以方便的获取到控件的各种尺寸。
JHDropDownMenu - 扩展UIView的下拉菜单
检查是否在正确的线程上调用了UIView方法。 。 安装 拖入项目 在导航器中选择项目,然后选择构建设置选项卡 使用“预处理器宏”进行过滤,然后使用左侧的箭头展开该行 双击调试行值,键入DEBUG=1 在调试行类型-...
Layoutable是UIView应用程序的扩展,使Auto Layout的使用更加容易。该框架不会替代UIKit中可用的任何现有自动布局API。 Anchor API非常好且易于使用,但是对于常见任务有时需要重复很多代码。该框架仅添加了几个有用...
UIView-Positioning, 在UIView对象中,基于简单属性的框架属性设置 uiview定位收费 UIView Positioning 是一个快速扩展,它提供简单的shorthand 方法,以方便的方式定义任何UIView对象的框架属性( 宽度,高度,x,y ...
CGFloat和UIView的实用扩展旨在增强布局代码的可读性
UIView的扩展。 类支持两种类型的动画:显示(当前)和点击(触摸)。 如果您在点击按钮,textField等时添加动画效果,则可以为UIControl类创建新的扩展,并覆盖方法touchesBegan(_:withEvent :)并在此处启动...
UIViewInspectable 用于在 Interface Builder 中将 cornerRadius、borderWidth 或 borderColor 设置为 IBInspectable 的 UIView 扩展。 灵感来自 。安装通过将其添加到您的 Podfile 来通过 cocoapods 安装: pod ...
Toast-Swift是一个Swift扩展,它将Toast通知添加到UIView对象类。 它旨在简单,轻巧且易于使用。 大多数吐司通知可以用一行代码触发。 Toast-Swift是的本机Swift端口。 屏幕截图 基本范例 // basic usage self . ...
一个 UIView 扩展,它允许你应用内部阴影而不必弄脏你的手。 这个项目或多或少是 Philip Yu 的的 Swift 端口。 安装 在 XCode 中将UIViewInnerShadow/UIViewInnerShadow.swift文件复制到您的项目中,您就完成了。 ...