在此之前我先简单的说一下frame 和bounds 的区别:frame 是该视图在父视图的位置,所谓的父视图就是我们在创建视图时将它添加显示在哪个视图上, bounds是该视图在自己视图上的位置
center 这个是中心点的位置 是CGPoint 类型
//视图的frame是依赖于父视图产生的坐标系统来确定其起始点和大小的
//视图的bounds是依赖于自身产生的坐标系统的,默认起始点的值为0,0与坐标系远点重合,size与frame指定的size一致.一旦修改了一个视图的bounds,不会影响视图的位置(frame不变)但是会让自身产生的坐标系统偏移,导致添加在其上的子视图产生相对位置的变化
//视图的frame和center共同参考父视图产生的坐标系.frame的origin(起始点)发生改变,size(大小)发生改变都会影响center (中心点) 的位置 .center的修改只会影响frame的origin.
NSLog(@"%@",NSStringFromCGRect(self.window.frame));
NSLog(@"%@",NSStringFromCGRect(yellowView.frame));
NSLog(@"%@",NSStringFromCGRect(yellowView.bounds));
//创建aView,设置frame
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)]
;
//改变背景颜色
aView.backgroundColor = [UIColor whiteColor];
//改变四角,就是四个角的内切圆半径,使视图四角由直角变为圆角
aView.layer.cornerRadius = 20;
//改变线框的颜色
aView.layer.borderColor = [[UIColor blackColor]CGColor];
//改变线框的粗细,这里的数字 单位是像素点
aView.layer.borderWidth = 2;
//加入到window视图上,window则是这个视图的父视图
[self.window addSubview:aView];
[aView release];
UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)]
;
bView.backgroundColor = [UIColor redColor];
bView.layer.cornerRadius = 20;
bView.layer.borderColor = [[UIColor blackColor]CGColor];
bView.layer.borderWidth = 1;
//这个是让该视图的中心点等于第一个视图的中心点
bView.center = aView.center;
[self.window addSubview:bView];
[bView release];
UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]
;
cView.backgroundColor = [UIColor orangeColor];
cView.center = bView.center;
cView.layer.cornerRadius = 20;
cView.layer.borderColor = [[UIColor blackColor]CGColor];
cView.layer.borderWidth = 2;
[self.window addSubview:cView];
[cView release];
//将window 的aView视图放在cView上
[self.window insertSubview:aView aboveSubview:cView];
//将window 的aview视图放在bview下
[self.window insertSubview:aView belowSubview:bView];
//将bview视图放在下标为2的位置上
[self.window insertSubview:bView atIndex:2];
//将cview放在最前面
[self.window bringSubviewToFront:cView];
//将bview放在最下面
[self.window sendSubviewToBack:bView];
//交换 下标为0,2的俩个视图位置
[self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:2];
//将aview视图 移除他所在的父视图
[aView removeFromSuperview];
NSLog(@"%@",NSStringFromCGRect(bView.frame));
NSLog(@"%@",NSStringFromCGRect(bView.bounds));
//显隐 当hidden 为YES视图消失,当为NO显示. 系统默认为NO;
cView.hidden = YES;
//标记 视图
bView.tag = 1000 ;
//取出视图
[self.window viewWithTag:1000];
//取出其子视图
NSArray *array1 = [self.window subviews];
//取出其父视图
NSArray *array2 = [bView superview];
本文详细介绍了iOS中视图的frame、bounds和center属性的概念及其相互关系,并通过实例展示了如何利用这些属性进行视图定位及层级管理。
345

被折叠的 条评论
为什么被折叠?



