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

来源:互联网 发布:iptv网络电视怎么用 编辑:程序博客网 时间:2024/05/16 09:11
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);
}
0 0
原创粉丝点击