`
jsntghf
  • 浏览: 2473325 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

UIView的扩展类

    博客分类:
  • iOS
阅读更多

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];

 

示例图:


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics