从父UIView的中心添加UIView

时间:2015-07-26 09:14:43

标签: ios uiview cgpoint

我无法理解如何从第二个UIView的中心开始向UIView添加UIView而不是左上角,我正在制作一个例子: 我想为此UIView添加CGPoint :( - 96,96),第二个UIView它是一个方形矩形300x300,所以我想从{1}开始添加UIView中心,而是从左上角开始添加UIVIew,当它被添加到屏幕外时,这是一个例子:

          ________________
          |              |
   y      |   x          |
          |       .      |
          |              |
          |              |
          |______________|

x = where i want should go the UIView
y = where instead it's added

我怎么能解决它?

编辑: 我以这种方式添加UIView:

UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
v.center = CGPointMake(-96,96);
[self.squareView addSubview:v];

1 个答案:

答案 0 :(得分:0)

试试这个:

UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
v.center = squareView.center;

var frame:CGRect = v.frame
frame.origin.x -= 96
frame.origin.y += 96
v.frame = frame

[self.squareView addSubview:v];