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扩展类"是一个方便的工具,可以极大地简化开发过程,特别是对于屏幕适配的问题。这个扩展类通常包含了一些实用方法,使得开发者能更高效地处理屏幕尺寸的变化,确保应用在不同设备上都能正常显示。 首先,`...
基于UIView扩展的类目,用于设置上下左右圆角,可设置单边圆角,也可设置为圆形 github地址:https://github.com/LQQZYY/UIViewCornerDemo 喜欢的话就给颗星支持一下,感谢!
UIView+Extension 对view的一个扩展
本篇文章将详细介绍如何在Objective-C(OC)和Swift中扩展`UIView`来实现虚线外边框的功能,以及如何通过类扩展的方式来封装这个功能,使得在实际项目中调用更为便捷。 首先,我们来看`UIView`的虚线外边框。虚线...
首先,扩展类代码展示了如何为UIView定义一些便捷的属性,如`origin`, `size`, `left`, `top`, `right`, `bottom`。这些属性使得开发者可以直接操作视图的坐标和尺寸,而无需每次都通过frame来访问。例如,`left`和`...
总结来说,`UIViewExt`工具类是iOS开发中的实用工具,通过扩展`UIView`类提供了丰富的便捷属性,使得获取和操作视图的坐标、尺寸等属性变得更加简单和直观,同时提高了代码的可读性和开发效率。在使用时,我们需要...
在实际开发中,`UIView+RectCorner`这样的扩展可以帮助我们快速实现视图的圆角效果,同时保持代码的简洁和可维护性。通过这个扩展,我们可以避免在多个地方重复编写相同的圆角设置代码,从而提高代码的复用性和一致...
本项目“通过协议扩展高斯模糊任意 UIView”提供了一种优雅的方式来为Swift中的UIView添加高斯模糊功能,无需为每个需要模糊的视图创建单独的类或方法。下面将详细解析这个开源项目的实现原理、使用方法及其优势。 ...
同时,由于它是对`UIView`的一个静态扩展,所以无需创建新的类或继承,也不会影响原有的类结构,符合苹果推荐的轻量级编程原则。 总的来说,这个`UIView+Extension`分类为iOS开发者提供了一种高效、简洁的方式来...
`UIView AutoLayout`是这个机制的一个扩展,为`UIView`类添加了分类,提供了更加简洁易用的接口来设置自动布局约束。这个压缩包"UIView AutoLayout.zip"包含的项目名为"UIView-AutoLayout-master",很可能是GitHub上...
在iOS开发中,扩展UIView以实现下拉菜单是一项常见的需求,尤其在构建用户界面时,下拉菜单可以提供一种简洁的交互方式,让用户在有限的空间内进行多项选择。本开源项目"JHDropDownMenu"正是针对这个需求而设计的,...
分类是Objective-C和Swift中的一种特性,允许我们扩展已有的类,添加新的方法、属性或协议,而不必继承该类。在Swift中,我们称之为扩展(Extension),但它与分类有着相似的功能。通过为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_开头的...
本教程将深入探讨如何使用扩展(Extension)和运行时(Runtime)技术,为UIView类添加手势识别功能,从而实现更丰富的用户交互体验。 首先,让我们了解什么是扩展。在Objective-C和Swift中,扩展是一种可以为已存在...
这种组件可能通过扩展UIView类或者创建一个类别(Category)来实现,以便在不改变原生UIView结构的情况下,增加显示进度的能力。 在Swift中,我们可以创建一个名为`Progressable`的协议,定义进度条的相关属性和...
`JHDropDownMenu`就是通过扩展UIView类,添加了一个方法来创建和展示下拉菜单。开发者只需要调用这个方法,传入必要的数据(如菜单项标题、图标等),即可实现下拉菜单的展示。 实现下拉菜单的关键在于处理触摸事件...
总结来说,`UIView+Utils`是对`UIView`类的扩展,提供了获取和操作视图尺寸的便利方法,以及与布局、动画相关的辅助功能,大大提升了iOS开发的效率。在使用时,开发者只需引入相应的库,就可以轻松调用这些方法,...
`KTOneFingerRotationGestureRecognizer`是一个自定义的手势识别类,它是对苹果内置的`UIGestureRecognizer`类的扩展。`UIGestureRecognizer`是iOS SDK中处理用户触摸事件的基础类,它提供了多种手势类型,如轻拍...