frame和bounds的区别
frame是指相对坐标,是自己在Super View的相对位置,是看自己以Super View的哪个位置为初始位置,然后以那一点为原点,原点不一定是(0,0)。
bounds是指绝对坐标,是自己的坐标系的坐标,以自己为原点的坐标系。原点坐标永远为(0,0);
举个例子说明:
代码如下:
UIButton *button;//按钮,往TagView添加多个按钮
CGFloat floatW =0;
for (int i=0; i<3; i++) {
button =[[UIButton alloc] initWithFrame:CGRectMake(floatW,25, 30, 15)];
[button.layer setMasksToBounds:YES];
[button.layer setCornerRadius:7];
button.layer.borderWidth =1;
button.layer.borderColor =DEF_TextColor.CGColor;
[button setTitle:[NSString stringWithFormat:@"标签%d",i] forState:UIControlStateNormal];
floatW +=35;
[self.TagView addSubview:button];
NSLog(@"bounds(%f,%f)-----------frame(%f,%f)",button.bounds.origin.x,button.bounds.origin.y,button.frame.origin.x,button.frame.origin.y);
}
输出的结果:bounds(0.000000,0.000000)-----------frame(0.000000,25.000000)
bounds(0.000000,0.000000)-----------frame(35.000000,25.000000)
bounds(0.000000,0.000000)-----------frame(70.000000,25.000000)
就是无论什么时候button的bounds原点坐标永远为(0,0),而frame的原点坐标为(floatW,25)随着floatW的变化而变化。

浙公网安备 33010602011771号