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

标签 ios uibutton constraints

这是我更新的问题!

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

我是 ios 的新手,所以无法为我的 UIButton 设置约束。这是我的 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]];

上面的代码设置在右上角。所以我已将其更改为底部,中心,但我看不到我的按钮。我需要我的按钮 Like this image button position无法设置约束以将我的 uibutton 位置放在同一个地方。任何人都可以帮助我解决我的问题

最佳答案

我已经检查了您的代码。将下面的代码替换为您的代码。您可以根据需要将按钮放在所需的位置

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

希望这对您有所帮助。请学习一些有关自动布局的教程。确保这些概念对制作应用程序非常有帮助...

关于ios - 需要在底部中心为我的 UIButton 设置自动布局约束使用代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32808065/

相关文章:

sql - Oracle SQL 中的检查约束

ios - managedObjectContext 返回 nil

ios - 在可移动的 UIView 中移动 UILabel

C++ 约束 enable_if 与 requires

ios - 如何在iOS中以编程方式设置按钮文本?

ios - UIButton 不可点击

MySQL-错误号 150 : "Foreign key constraint is incorrectly formed"

ios - UICollectionView 渲染得比 Storyboard 中的要高

ios - 在长 UIWebview 内容 iOS 9 中控制/处理嵌入式 youtube 视频

ios - 在 Swift 中点击并按住 UIButton?