ios 视图center的一个坑

来源:互联网 发布:sql server 分组排序 编辑:程序博客网 时间:2024/06/06 01:51

我们在进行视图布局的时候,有时候会出现设置的位置与预想的位置不一样,但看又看不出来问题。

现在说明我遇到的一个问题:

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]; }
原创粉丝点击