hridayesh
hridayesh

Reputation: 1143

Objective C: Adding UI button with constraints programatically

I want to create button of fixed dimension which is set to fix distance from top right regardless of screen size and orientation, But not able to achieve it.

I have tried following code which shows correctly in portrait mode only since frame location is fixed.

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.closeBtn addTarget:self action:@selector(closeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];

//NSLayoutConstraint* doneconstraint = [NSLayoutConstraint constraintWithItem:self.closeBtn attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:5.0];
//[self addConstraint:doneconstraint];
[self.view addSubview:self.closeBtn];

I have also tried adding constraints [uncomment 2 lines] but it gives following error

Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)

My guess for the error was that frame and constraint specifies different locations of button, but I am new to this so I am not sure what is going on and how to fix.

One solution would be to change frame on rotation but i thought using constraints it will be cleaner approach.

Upvotes: 0

Views: 4274

Answers (1)

Leo
Leo

Reputation: 24714

Try this code,just replace 60 to what you want

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

And Screenshot is

enter image description here

Upvotes: 4

Related Questions