将子UIView置于其父UIView中心

时间:2013-11-12 13:11:40

标签: ios iphone objective-c uiview

我想将子视图置于其父级中间,我已经查看了SO的答案,但到目前为止他们还没有帮助我。 This one specifically looks like it should work,但不是。

这就是我正在做的事情

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

以下是它的外观(灰色圆圈)

enter image description here

5 个答案:

答案 0 :(得分:1)

您必须将您的代码(2.示例)从viewDidLoad移至viewWillLayoutSubviews,并且该代码将被怀疑。

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

答案 1 :(得分:0)

我认为你应该向左移动半宽,向上移动半高:

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

但我会使用类似的东西:

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

我上传了一个演示https://github.com/luisespinoza/CenterViewTest

答案 2 :(得分: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];

答案 3 :(得分: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);

答案 4 :(得分: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会怎样?