UIView的层次调整,及子view布局模式自动布局模式(停靠模式)
2016-08-01 02:02
337 查看
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);
}
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布局模式自动布局模式(停靠模式)
- iOS 8自动调整UITableView和UICollectionView布局
- iOS UIViewd的Content Modes属性 调整 ImageView 图片自适应模式 展示样式 UIViewContentMode各类型效果
- iOS 8自动调整UITableView和UICollectionView布局
- iOS 8自动调整UITableView和UICollectionView布局
- iOS 8自动调整UITableView和UICollectionView布局
- android中的activity布局内含有webview,弹出软键盘如何自动适应屏幕,自动调整
- iOS8自动调整UITableView和UICollectionView布局---转自http://www.cnblogs.com/xubojoy/p/3933693.html#3016517
- iOS 8自动调整UITableView和UICollectionView布局
- Eclipse自动调整XML布局(好看、实用、规范)
- UIView层次管理(sendSubviewToBack,bringSubviewToFront)
- 布局控件"WeifenLuo.WinFormsUI.Docking"的使用--如何控制自动停靠窗口的大小
- android之自定义ViewGroup和自动换行的布局的实现
- (转)Android中View绘制优化之一---- 优化布局层次
- android之自定义ViewGroup和自动换行的布局的实现
- android之自定义ViewGroup和自动换行的布局的实现
- IOS UIView自动调整尺寸
- IOS UIView自动调整尺寸[转自sdk]
- [转]让UITableView中的单元格根据内容的多少自动调整高度
- android之自定义ViewGroup和自动换行的布局的实现