UIView简介及常用设置

来源:互联网 发布:港澳台电视直播软件tv 编辑:程序博客网 时间:2024/06/06 05:29
                                     **UIView**

一、简介
UIView是一个矩形的视图,用来展示所有的东西,
在iOS中,所有可见的控件都是UIView的子类或衍生类。
使用步骤:
1.创建对象;
2.设置属性;
3.扔到窗户上.

二、 定义及基本设置

1.定义(创建对象)
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];

2.设置背景颜色
view.backgroundColor = [UIColor cyanColor];

3.用UIWindow的坐标点,重新设值View的坐标原点:本来View自身的坐标原点在UIWindow中为(100,100);通 过下面的设置,变成了(50,50);
view.bounds = CGRectMake(50, 50, 200, 200);

4.将视图添加到Window中
[self.view addSubview:view];

5.重新定义视图的中心
view.center = CGPointMake(200, 300);

6.视图的中心与父视图的中心重合
view.center = self.view.center;

7.设置是否隐藏
view.hidden = NO;

8.设置透明度
视图的透明度设置是会传递到子视图中的;
view.alpha = 0.9;

9.tag 值
给这个对象一个标记号,后面可以根据这个标记号来取到这个视图;
view.tag = 119;//(大于100的)
UIView *v1 = [self.view viewWithTag:119];
v1.backgroundColor = [UIColor redColor];

10 superView
取到父视图的对象,给父视图定义背景颜色;
view.superview.backgroundColor = [UIColor orangeColor];

11.subView拿到所有的子视图
NSArray *arr = self.view.subviews;
for (UIView *vi in arr) {
vi.backgroundColor = [UIColor greenColor];
}

12.设置圆角
设置是否支持圆角
view.layer.masksToBounds = YES;
设置圆角半径
view.layer.cornerRadius = 50;

13.添加子视图
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view2.backgroundColor = [UIColor blueColor];
[view addSubview:view2];

14.插入视图
[self.view insertSubview:greenView aboveSubview:blueView];//插在…之上。
[self.view insertSubview:greenView belowSubview:blueView];//插在…zhixia

15.从里到外,从0开始逐渐增加
[self.view insertSubview:greenView atIndex:0];

16.把一个子视图带到最前面
[self.view bringSubviewToFront:blueView];

17.把一个子视图带到最后面
[self.view sendSubviewToBack:blueView];

18.将两个视图的位置进行交换
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:2];

19.将视图从父视图中移除出去
[redView removeFromSuperview];

20.在主背景里添加图片
UIImage *image = [UIImage imageNamed:@”5.jpg”];
UIImageView *vi = [[UIImageView alloc]initWithImage:image];
vi.frame = self.view.bounds;
[self.view addSubview:vi];

21.在View里添加图片
UIImageView *imv = [[UIImageView alloc] initWithFrame:CGRectMake(30, 30, 40, 40)];
imv.image = [UIImage imageNamed:@”1.jpg”];
[self addSubview:imv];

2 0
原创粉丝点击