2015-09-27 47 views
0

这是我更新的问题!需要设置底部中心的UIButton自动布局约束使用代码

我已经搜查了许多教程&网站设置自动布局约束在我看来controller.I使用代码底部中心设置我的UIButton创建的UIButton,我已经设置位置,但通常我可以看到我的UIButton在不同的模拟器(4S,5,6,冲击我自己的设备)的不同位置定位。我需要设置我的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]]; 

这上面的代码是为top-right.so设置的,我已经将它改为底部,居中,但我无法看到我的按钮。我需要我的按钮Like this image button position无法设置约束来将我的uibutton位置放置在同样place.Kindly任何一个能帮助我解决我的问题

+0

不要操纵框架,当你唱自动布局。只需调整约束。您需要在iOS中以编程方式搜索添加约束。这会给你足够的内容。但是你需要添加一些约束,比如pin到bottom,以superview,width和height为中心。 – Douglas

+0

我已更新我的post.still我的代码无法设置。请将任何一个帮助我out.i新ios – 2131

+0

我认为你有前两个倒退。您想要将所有约束添加到按钮,而不是视图。并且不要使用右侧或左侧,因为这会在每个设备上发生变化。使用中心。 – Douglas

回答

4

我有检查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]]; 

希望这有助于。请学习一些教程的自动layout.ure这个概念是非常有用的制作应用程序...

+0

是的,谢谢它的工作。 – 2131

相关问题