UIButton inside UIVIew not centered in iPhone app
<p>I'm actually setting a custom UIView programmatically that will contain several UIButton and this is working fine.
But what I'm trying to do is to center a UIButton inside a sub-custom UIView that I've created programmatically. Here is the code I'm actually using</p>

<pre><code>//UIView used as a parent for the blurrEffectView
blurredPowerDown = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
                    blurredPowerDown.backgroundColor = [UIColor clearColor];

                    //I'm calling this for applying my custom view on top of other application (yes this is jailbreak development but issue is not related at it at all
                    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
                    window.windowLevel = UIWindowLevelAlert + 2;
                    [window setHidden:NO];
                    [window setAlpha:1.0];
                    [window setBackgroundColor:[UIColor clearColor]];
                    [window addSubview:blurredPowerDown];

                    //Code to get blur depending of what is behind the view
                    blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
                    blurEffectView = [[UIVisualEffectView alloc]initWithEffect:blur];
                    blurEffectView.frame = blurredPowerDown.bounds;
                    blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
                    [blurEffectView setAlpha:0.0];
                    [blurredPowerDown addSubview:blurEffectView];

                    [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{

            [blurEffectView setAlpha:1.0];

            }
            completion:^(BOOL finished) {

            }];

                    //This is the view I use to center all the button that I will add (in red on the following picture)
                    centerView = [[UIView alloc] init];
                    [centerView setTranslatesAutoresizingMaskIntoConstraints:NO];
                    centerView.backgroundColor = [UIColor redColor];

                    [blurEffectView addSubview:centerView];

                    [blurEffectView addConstraint:[NSLayoutConstraint constraintWithItem:centerView
                                                      attribute:NSLayoutAttributeWidth
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:blurEffectView
                                                      attribute:NSLayoutAttributeWidth
                                                     multiplier:0.7
                                                       constant:0]];

                    [blurEffectView addConstraint:[NSLayoutConstraint constraintWithItem:centerView
                                                      attribute:NSLayoutAttributeHeight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:blurEffectView
                                                      attribute:NSLayoutAttributeHeight
                                                     multiplier:0.5
                                                       constant:0]];


                    [blurEffectView addConstraint:[NSLayoutConstraint constraintWithItem:centerView
                                                      attribute:NSLayoutAttributeCenterX
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:blurEffectView
                                                      attribute:NSLayoutAttributeCenterX
                                                     multiplier:1.0
                                                       constant:0.0]];


                    [blurEffectView addConstraint:[NSLayoutConstraint constraintWithItem:centerView
                                                      attribute:NSLayoutAttributeCenterY
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:blurEffectView
                                                      attribute:NSLayoutAttributeCenterY
                                                     multiplier:1.0
                                                       constant:0.0]];

                    //And this is the button that cause me issue
                    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
                    [cancelButton addTarget:self action:@selector(cancelView) forControlEvents:UIControlEventTouchUpInside];
                    [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
                    cancelButton.frame = CGRectMake(0, 0, 80, 80);
                    cancelButton.clipsToBounds = YES;
                    cancelButton.layer.cornerRadius = 80/2.0f;
                    cancelButton.layer.borderColor=[UIColor whiteColor].CGColor;
                    cancelButton.layer.borderWidth=2.0f;


                    [centerView addSubview:cancelButton];

                    //Constraints removed as per UditS suggestion
                    [centerView addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton
                                                      attribute:NSLayoutAttributeWidth
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:centerView
                                                      attribute:NSLayoutAttributeWidth
                                                     multiplier:1.0
                                                       constant:0]];

                    [centerView addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton
                                                      attribute:NSLayoutAttributeHeight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:centerView
                                                      attribute:NSLayoutAttributeHeight
                                                     multiplier:1.0
                                                       constant:0]];


                    [centerView addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton
                                                      attribute:NSLayoutAttributeCenterX
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:centerView
                                                      attribute:NSLayoutAttributeCenterX
                                                     multiplier:1.0
                                                       constant:0.0]];


                    [centerView addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton
                                                      attribute:NSLayoutAttributeCenterY
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:centerView
                                                      attribute:NSLayoutAttributeCenterY
                                                     multiplier:1.0
                                                       constant:0.0]];
</code></pre>

<p>Here is the screenshot of what it do</p>

<p><a href="https://i.stack.imgur.com/A4INP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/A4INP.png" alt="enter image description here"></a></p>

<p>I really don't understand why my UIButton always stay in the top left (like if it has no placement constraints but I have added them !)</p>

<p>I'm really desperated ! </p>

<p>Thanks in advance for your help </p>

<p>EDIT: If I set cancelButton.center = centerView.center; here is what I get:</p>

<p><a href="https://i.stack.imgur.com/AYmZE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AYmZE.png" alt="enter image description here"></a></p>

<p>EDIT2: As suggested, I've set cancelButton.translatesAutoresizingMaskIntoConstraints = NO; but here is the result:</p>

<p><a href="https://i.stack.imgur.com/NtwXC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NtwXC.png" alt="enter image description here"></a></p>

<p>EDIT3: we advance ^^ thanks to @UditS suggestion (delete the height and width constraints) the button is centered but now the border is very strange</p>

<p><a href="https://i.stack.imgur.com/GQk7L.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GQk7L.png" alt="enter image description here"></a></p>