需要在底部中心为我的 UIButton 设置自动布局约束使用代码

Need to set Auto layout constraint use code for my UIButton at bottom-centre

这是我更新的问题!

我搜索了很多教程和网站来设置自动布局约束以将我的 UIButton 设置在我视图的底部中心 controller.I 使用代码创建 uiButton 并且我已经设置了位置,但通常我可以看到我的uiButton 在不同的模拟器中定位在不同的位置(4S、5、6,INFACT 在我自己的设备上)。我需要将我的 uibutton 设置在底部中心 Like this image

我是 ios 的新手,所以无法为我的 UIButton.And 设置约束 这是我的 UIButton 代码:

self->closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self->closeBtn.frame = CGRectMake(260, 30, 50, 28);
self->closeBtn.layer.cornerRadius = 4;
self->closeBtn.layer.borderWidth = 1;
self->closeBtn.layer.borderColor = [UIColor colorWithRed:179.0/255.0 green:179.0/255.0 blue:179.0/255.0 alpha:1.0].CGColor;
[self->closeBtn setTitleColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0] forState:UIControlStateNormal];
self->closeBtn.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
[self->closeBtn setTitle:@"Done" forState:UIControlStateNormal];
[self->closeBtn.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:12.0]];
[self.view addSubview:self->closeBtn];
[self->closeBtn addTarget:self action:@selector(closeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
self->closeBtn.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint * c_1 =[NSLayoutConstraint constraintWithItem:self.view
                                                       attribute:NSLayoutAttributeRight
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:self->closeBtn
                                                       attribute:NSLayoutAttributeRight
                                                      multiplier:1.0 constant:60];
NSLayoutConstraint * c_2 =[NSLayoutConstraint constraintWithItem:self.view
                                                       attribute:NSLayoutAttributeTop
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:self->closeBtn
                                                       attribute:NSLayoutAttributeTop
                                                      multiplier:1.0 constant:-1*60];



NSLayoutConstraint * equal_w = [NSLayoutConstraint constraintWithItem:self->closeBtn
                                                            attribute:NSLayoutAttributeWidth
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:nil
                                                            attribute:0
                                                           multiplier:1.0
                                                             constant:70];
NSLayoutConstraint * equal_h = [NSLayoutConstraint constraintWithItem:self->closeBtn
                                                            attribute:NSLayoutAttributeHeight
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:nil
                                                            attribute:0
                                                           multiplier:1.0
                                                             constant:28];
[self.view addConstraints:@[c_1,c_2]];
[self->closeBtn addConstraints:@[equal_w,equal_h]];

上面的代码设置在顶部-right.so 我已将其更改为底部,中心但我看不到我的 button.i 需要我的按钮 Like this image button position 不是能够设置约束以将我的 uibutton 位置放在相同的位置 place.Kindly 任何人都可以帮助我解决我的问题

我已经用你的代码检查了你的code.Replace下面的代码。你可以根据需要将按钮放在所需的位置

UIView *superview = self.view;
// Do any additional setup after loading the view, typically from a nib.
    self->closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self->closeBtn.frame = CGRectMake(260, 100, 50, 28);
    self->closeBtn.layer.cornerRadius = 4;
    self->closeBtn.layer.borderWidth = 1;
    self->closeBtn.layer.borderColor = [UIColor colorWithRed:179.0/255.0 green:179.0/255.0 blue:179.0/255.0 alpha:1.0].CGColor;
    [self->closeBtn setTitleColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0] forState:UIControlStateNormal];
    self->closeBtn.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
    [self->closeBtn setTitle:@"Done" forState:UIControlStateNormal];
    [self->closeBtn.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:12.0]];
    [self.view addSubview:self->closeBtn];
    [self->closeBtn addTarget:self action:@selector(closeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    self->closeBtn.translatesAutoresizingMaskIntoConstraints = NO;

    NSLayoutConstraint * c_1 =[NSLayoutConstraint
                               constraintWithItem:self->closeBtn attribute:NSLayoutAttributeCenterX
                               relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
                               NSLayoutAttributeCenterX multiplier:1.0 constant:-7.5f];


    NSLayoutConstraint * c_2 =[NSLayoutConstraint
                               constraintWithItem:self->closeBtn attribute:NSLayoutAttributeCenterY
                               relatedBy:NSLayoutRelationEqual toItem:superview attribute:
                               NSLayoutAttributeCenterY multiplier:1.85f constant:0.0f];


    NSLayoutConstraint * equal_w = [NSLayoutConstraint constraintWithItem:self->closeBtn
                                                                attribute:NSLayoutAttributeWidth
                                                                relatedBy:NSLayoutRelationEqual
                                                                   toItem:nil
                                                                attribute:0
                                                               multiplier:1.0
                                                                 constant:50];
    NSLayoutConstraint * equal_h = [NSLayoutConstraint constraintWithItem:self->closeBtn
                                                                attribute:NSLayoutAttributeHeight
                                                                relatedBy:NSLayoutRelationEqual
                                                                   toItem:nil
                                                                attribute:0
                                                               multiplier:1.0
                                                                 constant:28];
    [self.view addConstraints:@[c_1,c_2]];
    [self->closeBtn addConstraints:@[equal_w,equal_h]];

希望这个helpful.Please学习一些汽车教程layout.sure这些概念对制作应用程序很有帮助...