UI之UI View--属性及用法

 1     // 设置视图view1,并初始化位置及大小
 2     UIView* view1 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
 3     // 设置背景颜色
 4     view1.backgroundColor = [UIColor blueColor];
 5     // 设置标签
 6     view1.tag = 1;
 7     // 设置透明度
 8     view1.alpha = 0.5;
 9     // 设置隐藏
10     view1.hidden = YES;
11     // 添加视图
12     [self.window addSubview:view1];
13     
14     // 设置另一个视图view2
15     UIView* view2 = [[UIView alloc]init];
16     // 设置位置及大小
17     view2.frame = CGRectMake(10, 10, 60, 60);
18     view2.backgroundColor = [UIColor redColor];
19     // 将view2添加到父view1中,CGRecMake是相对于view1左上角的相对位置
20     [view1 addSubview:view2];
21     
22     // 得到view2的中心点(point.x,point.y)
23     CGPoint point = view2.center;
24     // 得到view2的边框大小,bounds不能得到x和y
25     /* bounds.origin.x;
26     bounds.origin.y;
27     bounds.size.width;
28     bounds.size.height; */
29     CGRect bounds = view2.bounds;
30     // bounds与frame的联系:bounds.size改变时,frame.size也会发生变化;
31     // frame.size改变时,bounds.size也会发生变化
32     
33     // 设置视图view1,并初始化位置及大小
34     UIView* view3 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
35     // 设置背景颜色
36     view3.backgroundColor = [UIColor purpleColor];
37     // 添加视图
38     [view1 addSubview:view3];
39     
40     // 得到父视图
41     // UIView* superView = view2.superview;
42     UIView* superView = [view2 superview];
43     superView.backgroundColor = [UIColor grayColor];
44     // 得到子类视图组
45     NSArray* subViews = view1.subviews;
46     NSLog(@"count = %d",subViews.count);
47     
48     // 数组的顺序[最先加入到父视图的在数组中就是第一个,下标索引为0];
49     // 获取子视图组中下标为0的对象;
50     UIView* vie = [subViews objectAtIndex:0];
51     vie.backgroundColor = [UIColor greenColor];
52     
53     // 在指定的index处插入子视图
54     UIView* vi = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
55     [view1 insertSubview:vi atIndex:1];
56     
57     // 在指定的视图上面添加子视图
58     [view1 insertSubview:vi aboveSubview:view2];
59     
60     // 在指定的视图下面添加子视图
61     [view1 insertSubview:vi belowSubview:view2];
62     
63     // 把指定的子视图移动到最前面
64     [view1 bringSubviewToFront:vi];
65     
66     // 把指定的子视图移动到最后面
67     [view1 sendSubviewToBack:vi];
68     
69     // 交换两个指定索引位置的子视图
70     [view1 exchangeSubviewAtIndex:0 withSubviewAtIndex:2];
71     
72     // 把子视图从父视图上移除
73     [vi removeFromSuperview];
74     
75     // 自动裁剪[如果子类视图超出父视图,超出部分会被剪掉];
76     UIView* blueView = [[UIView alloc]initWithFrame:CGRectMake(10, 100, 300, 30)];
77     blueView.backgroundColor = [UIColor blueColor];
78     [view1 addSubview:blueView];
79     blueView.clipsToBounds = YES;
80     
81     // 允许子视图使用自动布局方式
82     blueView.autoresizesSubviews = YES;
83     // 设置自动布局方式
84     blueView.autoresizingMask = UIViewAutoresizingNone; // 设置左右、上下头可以改变
85     blueView.autoresizingMask = UIViewAutoresizingFlexibleWidth; // 改变宽
86     blueView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
87     blueView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
88     blueView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
89     blueView.autoresizingMask = UIViewAutoresizingFlexibleHeight; // 改变高
90     blueView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
原文地址:https://www.cnblogs.com/WillingToAsk1946zzh/p/4471479.html