在Objective-C中的ParentView中居中子视图

时间:2016-06-22 14:03:20

标签: ios

我有以下代码:

UIView *parentView = [[UIView alloc] init];
parentView.frame = CGRectMake(0, 0, 100, 100);

parentView.center = self.view.center;

parentView.backgroundColor = [UIColor greenColor];

UIView *subView = [[UIView alloc] init];

subView.backgroundColor = [UIColor redColor];

subView.frame = CGRectMake(0, 0, 50, 50);

subView.center = parentView.center;

[parentView addSubview:subView];

[self.view addSubview:parentView];

产生以下结果:

enter image description here

为什么红色视图不在绿色视图中居中,因为它们具有相同的中心?

4 个答案:

答案 0 :(得分:1)

Apple UIView文档说明center属性:

  

中心在其超视图的坐标系内指定,并以磅为单位进行测量。设置此属性会相应地更改框架属性的值。

这意味着parentView中心将相对于其超级视图(截图的白色背景视图)。

要获得所需的结果,您需要执行以下操作:

subview.center = CGPointMake(CGRectGetMidX(parentView.bounds),
                             CGRectGetMidY(parentView.bounds));

但你真的应该使用autolayout这类事情。

答案 1 :(得分:0)

您正在将绿色视图设置为其超级视图的中心,但您将红色视图设置为其超级视图的超级视图的中心......

使用最少的代码更改来解决这个问题的方法是更改​​红色视图的超级视图。

[self.view addSubview:parentView];
[self.view addSubview:subView];

编辑:进一步解释发生了什么。说当你设置绿色视图的中心时,它被设置为(500,500)。这是在它的超级视图中,所以它被设置在屏幕的中间。然后将红色视图的中心设置为与绿色视图中心相同的值(500,500)。但它的亲属是红色视图,因此它从绿色视图的原点放置(500,500)。它位于屏幕右下方附近。

答案 2 :(得分:0)

  

你这样做

UIView *parentView = [[UIView alloc] init];
parentView.frame = CGRectMake(0, 0, 100, 100);

parentView.center = self.view.center;

parentView.backgroundColor = [UIColor greenColor];


UIView *subView = [[UIView alloc] init];

subView.backgroundColor = [UIColor redColor];

CGFloat SubviewX = (parentView.frame.size.width - 50)/2;
CGFloat SubviewY = (parentView.frame.size.height - 50)/2;

subView.frame = CGRectMake(SubviewX, SubviewY, 50, 50);


[parentView addSubview:subView];
[self.view addSubview:parentView];

答案 3 :(得分:0)

即使视图更改其视图框架,使视图居中并以此方式保持视图的最佳方法是使用约束:

UIView *parentView = [[UIView alloc] init];
parentView.translatesAutoresizingMaskIntoConstraints = NO;

// You might want to add constraints for the view's size
parentView.frame = CGRectMake(0, 0, 100, 100);

parentView.backgroundColor = [UIColor greenColor];

[self.view addSubview:parentView];

NSContraint *xContraint = [NSLayoutConstraint constraintWithItem:parentView
                                                       attribute:NSLayoutAttributeCenterX
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:self.view
                                                       attribute:NSLayoutAttributeCenterX
                                                      multiplier:1.0
                                                        constant:0.0];

[self.view addConstraint:xContraint];

NSContraint *yContraint = [NSLayoutConstraint constraintWithItem:parentView
                                                       attribute:NSLayoutAttributeCenterY
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:self.view
                                                       attribute:NSLayoutAttributeCenterY
                                                      multiplier:1.0
                                                        constant:0.0];

[self.view addConstraint:yContraint];

UIView *subView = [[UIView alloc] init];
subView.translatesAutoresizingMaskIntoConstraints = NO;

// You might want to add constraints for the view's size
subView.frame = CGRectMake(0, 0, 50, 50);

subView.backgroundColor = [UIColor redColor];

[parentView addSubview:subView];

NSContraint *xSubContraint = [NSLayoutConstraint constraintWithItem:subView
                                                          attribute:NSLayoutAttributeCenterX
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:parentView
                                                          attribute:NSLayoutAttributeCenterX
                                                         multiplier:1.0
                                                           constant:0.0];

[parentView addConstraint:xSubContraint];

NSContraint *ySubContraint = [NSLayoutConstraint constraintWithItem:subView
                                                          attribute:NSLayoutAttributeCenterY
                                                           relatedBy:NSLayoutRelationEqual
                                                             toItem:parentView
                                                          attribute:NSLayoutAttributeCenterY
                                                         multiplier:1.0
                                                           constant:0.0];

[parentView addConstraint:ySubContraint];