//UIView - 视图 基础单元 UI的基类 UIView: UIResponder : NSObject 继承关系
// 1. 位置和大小 2. 加载在父视图中
// 320 x 480 px 状态栏 20px iphone4s
#if 0
// blue
UIView *view1 = [[UIView alloc] init]; // 默认透明色
// 位置大小
// view1.frame = CGRectMake(10, 30, 300, 440);
view1.frame = self.view.bounds;
// 可以修改中心点的位置,来控制视图响应的移动
// 设置背景颜色
view1.backgroundColor = [UIColor blueColor];
[self.view addSubview:view1]; // 父视图添加子视图
// red
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
view2.backgroundColor = [UIColor redColor];
[self.view addSubview:view2];
// 如果多个子视图在同一个父视图中,后加入的控件盖在上面
// 判定是否为某视图的子视图
BOOL isSubView = [view1 isDescendantOfView:view2];
if (isSubView) {
NSLog(@"view1是view2的子视图");
} else {
NSLog(@"view1不是view2的子视图");
}
// orange
UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
view3.backgroundColor = [UIColor greenColor];
[view1 addSubview:view3];
// 1.坐标是相对于‘父视图’来设置的
// 2.遮挡关系是根据最大的父视图来进行判定,再逐一判定每个子视图的子视图
// 调整遮挡关系 加入父视图的顺序
// 1.将某一个子视图放入最上层
[self.view bringSubviewToFront:view1]; // 父视图 调 子视图
// 2.将某一个子视图放入最下层
[self.view sendSubviewToBack:view1];
// 3.交换两个层的视图
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
// 4.将子视图加入到某一个子视图的下层
// gray
UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(30, 20, 50, 50)];
view4.backgroundColor = [UIColor yellowColor];
// 将view4加到view2的下方
[self.view insertSubview:view4 belowSubview:view2];
// 5.将子视图加入到某一个子视图的上层
// black
UIView *view5 = [[UIView alloc] initWithFrame:CGRectMake(5, 10, 100, 30)];
view5.backgroundColor = [UIColor blackColor];
[self.view insertSubview:view5 aboveSubview:view2];
// 6.将子视图加入到特定层
// white
UIView *view6 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 50, 50)];
view6.backgroundColor = [UIColor whiteColor];
[self.view insertSubview:view6 atIndex:4];
// 7.求frame 和 bounds
// frame
NSLog(@"%.1f %.1f %.1f %.1f",view6.frame.origin.x,view6.frame.origin.y,view6.frame.size.width,view6.frame.size.height);
// bounds 通过bounds只能获取宽高
NSLog(@"%.1f %.1f %.1f %.1f",view6.bounds.origin.x,view6.bounds.origin.y,view6.bounds.size.width,view6.bounds.size.height);
// center
NSLog(@"%.1f %.1f",view6.center.x,view6.center.y);
// 获得父视图 一个
UIView *superView = view1.superview;
NSLog(@"superView = %@",superView);
// 获得子视图 数组
NSArray *subViews = view1.subviews;
NSLog(@"subViews = %@",subViews);
// tag 标示 找到某一个子视图
view1.tag = 1001;
// 通过tag找子视图
UIView *tagView = [self.view viewWithTag:1001];
tagView.backgroundColor = [UIColor lightGrayColor]; // 浅灰色
/**
<1. iOS6之前 UIView自适应来做屏幕适配
<2. iOS6->iOS8 AutoLayout
<3. iOS8之后 AutoLayout + SizeClass
*/
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(110, 200, 100, 100)];
backView.backgroundColor = [UIColor yellowColor];
backView.tag = 1002;
// 准许子视图跟随改变
backView.autoresizesSubviews = YES;
[self.view addSubview:backView];
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(25, 25, 50, 50)];
topView.backgroundColor = [UIColor greenColor];
// 设置子视图的跟随方式!
topView.autoresizingMask =
UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleWidth|
UIViewAutoresizingFlexibleHeight;
[backView addSubview:topView];
// nil 指针不存在 null 指针存在 但值是空的
// 定时器
/**
timeTick 执行的方法
TimeInterval 间隔时间
repeats 是否重复
*/
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeTick) userInfo:nil repeats:YES];
// 剪裁超出父视图范围的子视图
view1.clipsToBounds = YES;
#endif
UIView *view1 = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
view1.tag = 1;
view1.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:view1];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
view2.tag = 2;
view2.backgroundColor = [UIColor redColor];
[view1 addSubview:view2];
UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 20, 20)];
view3.tag = 3;
view3.backgroundColor = [UIColor greenColor];
[view2 addSubview:view3];
NSLog(@"%@",[view1 superview]);
for (UIView *view in [self.view subviews]) {
NSLog(@"self.viewSubviews = %d",view.tag);
}
for (UIView *view in [view1 subviews]) {
NSLog(@"view1 Subviews = %d",view.tag);
}
UIView基础
最新推荐文章于 2020-11-11 13:50:39 发布