0

我想将一个子视图放在其父视图的中间,我已经查看了关于 SO 的答案,但到目前为止它们并没有帮助我。 这个特别看起来应该可以工作,但没有。

这是我正在做的

-(void)viewDidLoad {
    [super viewDidLoad];

    finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    finalNumberCircle.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
    finalNumberCircle.Color = [UIColor darkGrayColor];
    [self.view addSubview:finalNumberCircle];
}

我还尝试了以下方法:

-(void)viewDidLoad {
    [super viewDidLoad];

    CGPoint translatedP = [self.view convertPoint:self.view.center fromView:self.view.superview];
    finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 100, 100];
    finalNumberCircle.center = translatedP;
    [self.view addSubView:finalNumberCircle];
}

这是它的外观(它是灰色圆圈)

在此处输入图像描述

4

5 回答 5

1

您必须将您的代码(2.示例)从viewDidLoadto移动viewWillLayoutSubviews,它会像怀疑的那样工作。

在调用 viewWillLayoutSubview 之前,您的 viewcontroller 视图边界仍然可以更改。

于 2013-11-12T13:34:53.713 回答
0

试试下面的代码...

[self.view addSubView:finalNumberCircle];

finalNumberCircle.frame=CGRectMake(finalNumberCircle.frame.origin.x+(self.view.frame.size.width/2-finalNumberCircle.center.x), finalNumberCircle.frame.origin.y+(self.view.frame.size.height/2-finalNumberCircle.center.y), finalNumberCircle.frame.size.width, finalNumberCircle.frame.size.height);
于 2013-11-12T13:22:12.383 回答
0

另一种可能性如何做到这一点(如果您需要特定的侧面偏移)。只需提供从父视图侧的偏移量,它就会在父视图的中心自动调整大小,并且还会自动调整大小,以防父视图框架发生更改。

- (void)viewDidLoad 
{
    [super viewDidLoad];

    CGFloat mOffsetFromSide = 50;

    finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x+mOffsetFromSide, self.view.bounds.origin.y+mOffsetFromSide, self.view.bounds.size.width-mOffsetFromSide*2, self.view.bounds.size.height-mOffsetFromSide*2];

    [finalNumberCircle setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];

    [self.view addSubView:finalNumberCircle];
}

如果您只想将视图定位在其父视图的中心,使其框架保持不变,则:

finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

finalNumberCircle.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);

[self.view addSubview:finalNumberCircle]; 

是的 - 它是您提供的代码的精确副本。它对我有用。

也许问题出在CircleView自己身上。或者您稍后在代码中更改它的框架?

如果您将“ CircleView”更改为简单 会发生什么UIView

于 2013-11-12T13:27:06.683 回答
0

I think that you should move half width to the left, and half height upwards:

-(void)viewDidLoad {
    [super viewDidLoad];
    finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(self.view.center.x-50, self.view.center.y-50, 100, 100]; 
    [self.view addSubView:finalNumberCircle];
}

But I would use something like:

#DEFINE CIRCLE_RADIUS    50

-(void)viewDidLoad {
    [super viewDidLoad];
    finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(self.view.center.x-CIRCLE_RADIUS, self.view.center.y-CIRCLE_RADIUS, 2*CIRCLE_RADIUS, 2*CIRCLE_RADIUS]; 
    [self.view addSubView:finalNumberCircle];
}

I uploaded a demo https://github.com/luisespinoza/CenterViewTest

于 2013-11-12T13:15:38.573 回答
0

当您想将坐标从一个视图的坐标转换为另一个时使用的 convertPoint 和相关方法,这不是您的情况。您必须执行以下操作:

finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
finalNumberCircle.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
[self.view addSubview:finalNumberCircle];
于 2013-11-12T13:15:55.643 回答