我们在进行视图布局的时候,有时候会出现设置的位置与预想的位置不一样,但看又看不出来问题。
现在说明我遇到的一个问题:
A、 期待位置
B、实际位置(有所偏移)
1、我在一个ViewController里创建一个视图UIView,并将该视图的中心设置为 ViewController 的center;
- (void)viewDidLoad {
[super viewDidLoad];
//在根控制器添加一个View
CGRect frame = CGRectMake(0, 0, 200, 200);
UIView *subView = [[UIView alloc]initWithFrame:frame];
subView.backgroundColor = [UIColor purpleColor];
//设置中心坐标
subView.center = self.view.center;
[self.view addSubview:subView];
UIView *sv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
sv.backgroundColor = [UIColor greenColor];
//这样写是有问题的
// sv.center = subView.center;
//这样写是ok的
sv.center = [subView convertPoint:subView.center fromView:self.view];
[subView addSubview:sv];
}