center属性是指当前view在其父view上的位置,center.x和center.y是以父view为参考系的。
举个例子
UIView * view1 =[[UIView alloc]initWithFrame:CGRectMake(40, 100, 100, 100)];
view1.backgroundColor =[UIColor orangeColor];
[self.view addSubview:view1];
NSLog(@"%f",view1.center.x);
NSLog(@"%f",view1.center.y);
//
UIView * view2 =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
view2.backgroundColor =[UIColor redColor];
view2.center = view1.center;
[view1 addSubview:view2];
打印出来的center.x =90, center.y=150;
此时若设置view2.center = view1.center; 因为view2的父view是view1,所以view2在view1上的中心坐标是(90,150),其实就不在view2的中点了。
当我们想在一个view上添加一个和它中心对齐的view2时,设置
view2.center =CGPointMake(view1.frame.size.width/2,view1.frame.size.height/2);即可