2015-09-27 62 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