vendredi 8 juillet 2016

Center SubViews in ParentView in Objective-C


I have the following code:

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

Which produces the following result:

enter image description here

Why is the red view not centered in the green view since they have the same center?


Aucun commentaire:

Enregistrer un commentaire