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

时间:2015-09-27 12:47:06

标签: ios uibutton constraints

这是我更新的问题!

我搜索了许多教程和设置自动布局约束的网站将我的UIButton设置在我的视图控制器的底部中心。我使用代码创建uiButton并且我已设置位置,但通常我可以看到我的uiButton位于不同模拟器中的不同位置(4S,5 ,6,感染我自己的设备)。我需要将我的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]];

以上代码设置为top-right.so我已将其更改为bottom,center但我无法看到我的button.i需要我的按钮Like this image button position无法设置约束来放置我的uibutton在同一个地方的位置。所以任何人都可以帮助我解决我的问题

1 个答案:

答案 0 :(得分:4)

我已检查您的代码。请使用您的代码将此代码放在下面。您可以根据需要将按钮放在位置

wine

希望这有用。请学习一些auto layout.sure的教程。这个概念对于制作App非常有帮助...