码迷,mamicode.com
首页 > 其他好文 > 详细

UIView的层次调整,及子view布局模式自动布局模式(停靠模式)

时间:2016-08-01 06:52:42      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

UIView*view1=[[UIView alloc]initWithFrame:CGRectMake(10,30,300,30)];

view1.backgroundColor=[UIColor redColor];
[self.window addSubview:view1];
[view1 release];
 
UIView*view2=[[UIView alloc]init];
view2.frame=CGRectMake(30,20,50,100);
view2.backgroundColor=[UIColor blueColor];
[self.window addSubview:view2];
[view2 release];
 
UIView*view3=[[UIView alloc]initWithFrame:CGRectMake(20,50,200,200)];
view3.backgroundColor=[UIColor yellowColor];
[self.window addSubview:view3];
//把某一个view放到最下层
[self.window sendSubviewToBack:view2];
//把某一个view放到最上层
[self.window bringSubviewToFront:view2];
//把某一个view加入到指定层
[self.window insertSubview:view2 atIndex:1];
//把某一个view加入到某层的下面
[self.window insertSubview:view2 belowSubview:view1];
//把某一个view加入到某层的上面
[self.window insertSubview:view2 aboveSubview:view1];
//交换两个层的view
[self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
 
//自动布局模式(停靠模式)
//前面把_backgroundView设置成了成员变量,为了方便,没有写出来
_backgroundView=[[UIView alloc]initWithFrame:CGRectMake(110,300,100,100)];
_backgroundView.background=[UIColor blackColor];
//设置父view允许子view自动布局
_backgroundView.autoresizesSubviews=YES;
[self.window addSubview:_backgroundView];
 
UIView*topView=[[UIView alloc]initWithFrame:CGRectMake(25,25,50,50)];
topView.backgroundColor=[UIColor orangeColor];
//设置子view的自动布局模式
//下面设置会让topView跟着_backgroundView变化而变化,中心点不变
topView.autoresizingMask=UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth;
[_backgroundView addSubview:topView];
//创建一个按钮,点一下,_backgroundView会变大
UIButton*btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame=CGRectMake(10,230,300,20);
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btn];
 
-(void)click
{
_backgroundView.frame=CGRectMake(_backgroundView.frame.origin.x-2,_backgroundView.frame.orgin.y-2,_backgroundView.frame.size.width+4,_backgroundView.frame.height+4);
}

UIView的层次调整,及子view布局模式自动布局模式(停靠模式)

标签:

原文地址:http://www.cnblogs.com/youlechang123/p/5724464.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!