Men的博客

欢迎光临!

0%

UIView

创建

UIView *view=[[UIView alloc]init];

坐标

view.frame=CGRectMake(100, 100, 100, 100);

中心位置

view.center=CGPointMake(200, 200);

大小 不想改变位置,想改变大小(设置bounds)

view.bounds=CGRectMake(0, 0, 50, 50);

tag值 区分不同控件(区分多个按钮)

view.tag=100;

是否打开用户交互

有的控件UIImageView默认不支持
后果:按钮直接加到UIImageView无法点击
view.userInteractionEnabled=YES;

视图之间的相互组合嵌套

[view addSubview:view1]

视图的层次感(层级关系)

视图层次感规律:先加入的在下面,后加入的在上面。

  1. 获取window的所有子视图
    NSLog(@”%@”,self.window.subviews);

UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(100, 240, 100, 100)];
view1.backgroundColor=[UIColor redColor];
[self.window addSubview:view1];
UIView *view2=[[UIView alloc]initWithFrame:CGRectMake(150, 310, 100, 100)];
view2.backgroundColor=[UIColor greenColor];
[self.window addSubview:view2];
UIView *view3=[[UIView alloc]initWithFrame:CGRectMake(200, 360, 100, 100)];
view3.backgroundColor=[UIColor blueColor];
[self.window addSubview:view3];
2.把view2放到最上面
[self.window bringSubviewToFront:view2];
3.把view2放到最下面
[self.window sendSubviewToBack:view2];
4.把某个视图插入到某个视图上面
UIView *view4=[[UIView alloc]initWithFrame:CGRectMake(120, 260, 100, 100)];
view4.backgroundColor=[UIColor purpleColor];
把视图插入到某个视图上面
[self.window insertSubview:view4 belowSubview:view1];
5.交换两个层
[self.window exchangeSubviewAtIndex:1 withSubviewAtIndex:2];
6.图层是可以放进数组里面
NSArray *subViews=self.window.subviews;
注意:当你交换了两个图层位置时,数组中相应的位置也会发生改变。
7.隐藏一个视图
view4.hidden=YES;
8.删除一个视图。
[view4 removeFromSuperview];