常见的iphone 尺寸:
iphone 3gs 3.5寸: 320 x 480 @1x
iphone 4 & 4s 3.5寸 320 x 480 @2x 640 X 960
iphone 5 5c 5s 320X 568 @2x 640 X 1136
iphone 6 4.7寸 375x 667 @2x 750x1344
iphone 6s 5.5 414 x 736 @3x 1242x2208
UIView 常用方法:
UIView* view1 = [[UIView alloc] init];
view1.frame = CGRectMake(10, 30, 350, 627);//状态栏高度为20px
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];
NSLog(@"x: %f, y: %f, w: %f, h: %f", view1.frame.origin.x, view1.frame.origin.y, view1.frame.size.width, view1.frame.size.height);
// bound - 边框大小,x,y永远为零
NSLog(@"x: %f, y: %f, w: %f, h: %f", view1.bounds.origin.x, view1.bounds.origin.y, view1.bounds.size.width, view1.bounds.size.height);
// center - 中心点
NSLog(@"center X: %f, Y: %f", view1.center.x, view1.center.y);
//父视图
UIView* superView = view1.superview;
superView.backgroundColor = [UIColor greenColor];
// 坐标根据父视图来设置的,不会垮层
UIView* view2 = [[UIView alloc]init];
view2.frame = CGRectMake(10, 100, 300, 30);
view2.backgroundColor = [UIColor blackColor];
view2.tag = 2; // 唯一特征值,用于区分
[view1 addSubview:view2];
UIView* view3 = [[UIView alloc]init];
view3.frame = CGRectMake(20, 50, 100, 100);
view3.backgroundColor = [UIColor purpleColor];
view3.tag = 3;
[view1 addSubview:view3];
// 子视图
// 1. 通过遍历,比较tag获取特定子视图---比较麻烦
NSArray* subViewArray = [view1 subviews];
for(UIView* view in subViewArray){
if(view.tag == 2){
view.backgroundColor = [UIColor whiteColor];
}
}
//2. 通过调用 viewWithTag 来获取
UIView* subview = [view1 viewWithTag:3];
subview.backgroundColor = [UIColor orangeColor];
// 层级的处理
// 1. 同一个父视图中的先加入的view 会被盖在下面
// 2. 不同的父视图, 其子视图优先级和父视图挂钩
// UIView* view4 = [[UIView alloc]init];
// view4.frame = CGRectMake(100, 100, 300, 300);
// view4.backgroundColor = [UIColor yellowColor];
// [self.view addSubview:view4];
// 3. 调整层级, 必须填写正确的层数
[view1 exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
// 4. 插入指定层
UIView* view5 = [[UIView alloc]init];
view5.frame = CGRectMake(7, 80, 200, 200);
view5.backgroundColor = [UIColor blackColor];
[view1 insertSubview:view5 atIndex:1];
NSLog(@"before");
//自适应
UIView* backView = [[UIView alloc]init];
backView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 25 , 400, 50, 50);
backView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:backView];
UIView* topView = [[UIView alloc]init];
NSLog(@"到这里了");
topView.frame = CGRectMake(10, 10, 30, 30);
topView.backgroundColor = [UIColor blackColor];
[backView addSubview:topView];
NSArray* array3 = [backView subviews];
if(array3.count > 0){
NSLog(@"加上了");
}
else{
NSLog(@"没加上");
}