今天测试了一个关于bounds的demo,发现了之前一直不知道的问题
// Do any additional setup after loading the view. UIView * view=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 200, 200)]; view.bounds=CGRectMake(-20, -20, 200, 200); NSLog(@"%f,%f",view.frame.origin.x,view.frame.origin.y); // view.center=CGPointMake(160, 240); view.backgroundColor=[UIColor redColor]; [self.view addSubview:view]; UIView * l=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; l.backgroundColor=[UIColor cyanColor]; [view addSubview:l];
代码就是这么多。
1.如果不设置view的frame。而只设置view的bounds,view的默认中心在原点上,也就是左上角。
2.view的bounds的前边两个参数是代表本地坐标。也就是说如上边例子中 (-20 ,-20 )代表如view的左上角相对于view 自己的坐标,那么(0,0)就如下图所示
在self。windows 的坐标系中从左上角坐标依次是 (0,0) ,(10,10)。。(30,30)
在view自己的本地坐标系中,从左上角坐标依次是(-20,-20),(0,0);