当前位置: 移动技术网 > 移动技术>移动开发>IOS > ios 视图center的一个坑

ios 视图center的一个坑

2018年09月13日  | 移动技术网移动技术  | 我要评论

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

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

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];
 }

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网