How to use constraints Programmatically? [closed] - ios

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am working on application which will work only horizontally and doing everything programmatically.
I am having a imageview, two textfield and two buttons but I don't know how to set constraints on these textfields and buttons, means when I am using in iPhone 5s then it is fine but when I am using iPhone 6s plus then it looks small.
I am posting here some code and screenshots
UIImageView *logoImage = [[UIImageView alloc]init];
logoImage.frame = CGRectMake(CGRectGetMidX(self.view.frame)-100, 10, 250, 100);
[logoImage setImage:[UIImage imageNamed:#"antya_logo1.png"]];
[self.view addSubview:logoImage];
UILabel *loginLabel = [[UILabel alloc]init];
loginLabel.frame = CGRectMake(10, 0, 100, 40);
loginLabel.text = #"Login Form";
loginLabel.textColor = [UIColor blackColor];
loginLabel.font = [UIFont fontWithName:#"LaoSangamMN" size:18];
CGFloat Xuser = CGRectGetMidX(self.view.frame)-120;
CGFloat Yuser = CGRectGetMaxY(loginLabel.frame)+80;
usernameField = [[UITextField alloc]init];
usernameField.frame = CGRectMake(Xuser, Yuser, 300, 35);
usernameField.placeholder = #" User Name";
usernameField.font = [UIFont fontWithName:#"LaoSangamMN" size:18];
usernameField.backgroundColor = [UIColor whiteColor];
usernameField.layer.cornerRadius = 7;
usernameField.layer.borderWidth = 0.5;
[self.view addSubview:usernameField];
UIImageView *userImgV = [[UIImageView alloc]init];
userImgV.frame = CGRectMake(CGRectGetMinX(usernameField.frame)-35, CGRectGetMinY(usernameField.frame)+5, 25, 25);
[userImgV setImage:[UIImage imageNamed:#"user-icon.png"]];
[self.view addSubview:userImgV];
CGFloat Ypass = CGRectGetMaxY(usernameField.frame)+20;
passwordField = [[UITextField alloc]init];
passwordField.frame = CGRectMake(Xuser, Ypass, 300, 35);
passwordField.placeholder = #" Password";
passwordField.secureTextEntry = YES;
passwordField.font = [UIFont fontWithName:#"LaoSangamMN" size:18];
passwordField.backgroundColor = [UIColor whiteColor];
passwordField.layer.cornerRadius = 7;
passwordField.layer.borderWidth = 0.5;
[self.view addSubview:passwordField];
this is in i phone 6s plus
and this is in i phone5s
Please help me,thanks in advance

Note : When you are dealing with autolayout setting frame of a view is not going to work. You need to set constraints for that view to look correctly in all iPhone devices.
UIImageView *logoImage = [[UIImageView alloc] init];
[logoImage setImage:[UIImage imageNamed:#"antya_logo1.png"]];
logoImage.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:logoImage];
UILabel *loginLabel = [[UILabel alloc]init];
loginLabel.text = #"Login Form";
loginLabel.textColor = [UIColor blackColor];
loginLabel.font = [UIFont fontWithName:#"LaoSangamMN" size:18];
loginLabel.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:loginLabel];
UITextField *usernameField = [[UITextField alloc]init];
usernameField.placeholder = #" User Name";
usernameField.font = [UIFont fontWithName:#"LaoSangamMN" size:18];
usernameField.backgroundColor = [UIColor whiteColor];
usernameField.layer.cornerRadius = 7;
usernameField.layer.borderWidth = 0.5;
usernameField.translatesAutoresizingMaskIntoConstraints = false;
UIImageView *userImgV = [[UIImageView alloc]init];
[userImgV setImage:[UIImage imageNamed:#"user-icon.png"]];
//set left view of textfield
usernameField.leftView = userImgV;
usernameField.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:usernameField];
UITextField *passwordField = [[UITextField alloc]init];
passwordField.placeholder = #" Password";
passwordField.secureTextEntry = YES;
passwordField.font = [UIFont fontWithName:#"LaoSangamMN" size:18];
passwordField.backgroundColor = [UIColor whiteColor];
passwordField.layer.cornerRadius = 7;
passwordField.layer.borderWidth = 0.5;
passwordField.translatesAutoresizingMaskIntoConstraints = false;
//set left view of textfield
UIImageView *passwordImgV = [[UIImageView alloc]init];
[passwordImgV setImage:[UIImage imageNamed:#"password-icon.png"]];
passwordField.leftView = passwordImgV;
passwordField.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:passwordField];
UIButton *buttonSignUp = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonSignUp setTag:101];
[buttonSignUp setTitle:#"SIGNUP" forState:UIControlStateNormal];
[buttonSignUp addTarget:self action:#selector(<your selector>) forControlEvents:UIControlEventTouchUpInside];
buttonSignUp.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:buttonSignUp];
UIButton *buttonFP = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonFP setTag:101];
[buttonFP setTitle:#"SIGNUP" forState:UIControlStateNormal];
[buttonFP addTarget:self action:#selector(<your selector>) forControlEvents:UIControlEventTouchUpInside];
buttonFP.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:buttonFP];
//setting constraints
//logoImage
//leading
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:logoImage attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:10]];
//Top
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:logoImage attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]];
//traling
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:logoImage attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:10]];
// usernameField
//leading
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:usernameField attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:10]];
//traling
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:usernameField attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:10]];
//top
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:logoImage attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:usernameField attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]];
//passwordField
//leading
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:passwordField attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:10]];
//traling
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:passwordField attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:10]];
//top
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:passwordField attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:passwordField attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]];
//buttonSignUp and buttonFP
//leading for buttonSignUp
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonSignUp attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:10]];
//traling for buttonFP
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:buttonFP attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:10]];
//equal width for both
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonFP attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:buttonSignUp attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
//space between both btns
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonFP attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:buttonSignUp attribute:NSLayoutAttributeRight multiplier:1.0 constant:10]];
//top for both
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonSignUp attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:passwordField attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonFP attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:passwordField attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]];
//bottom
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:buttonFP attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:buttonSignUp attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10]];

After adding #IBOutlet each of items on ViewController. You can use Auto Layout Visual Format Language to implement programmatically constraints. Here's sample code to understand from my project.
(Swift Version) If cell awakeFromNib or or page you can use in viewDidLoad:
avaImg.translatesAutoresizingMaskIntoConstraints = false
usernameBtn.translatesAutoresizingMaskIntoConstraints = false
infoLbl.translatesAutoresizingMaskIntoConstraints = false
dateLbl.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-10-[ava(30)]-10-[username]-7-[info]-10-[date]",
options: [], metrics: nil, views: ["ava":avaImg, "username":usernameBtn, "info":infoLbl, "date":dateLbl]))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-10-[ava(30)]-10-|",
options: [], metrics: nil, views: ["ava":avaImg]))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-10-[username(30)]",
options: [], metrics: nil, views: ["username":usernameBtn]))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-10-[info(30)]"
, options: [], metrics: nil, views: ["info":infoLbl]))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-10-[date(30)]",
options: [], metrics: nil, views: ["date":dateLbl]))
Additional link cited before Using Autolayout Visual Format with Swift?
(Objective-C Version) You can find here many details to do same.

Related

Horizontally Placed Equal Width Equal Horizontal Space Buttons With Auto Layout

I have written the following code to achieve
Horizontally spaced equal width fixed height equal horizontal spaced three button but somehow it's not working fine. Can anyone rectify this ?
UIButton *cancelBtn = [UIButton new];
cancelBtn.frame = CGRectMake(10, 5, (popupView.bounds.size.width-40)/3, 30);
[cancelBtn setTitle:#"Cancel" forState:UIControlStateNormal];
[cancelBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[cancelBtn setBackgroundColor:[UIColor lightGrayColor]];
[popupView addSubview:cancelBtn];
UIButton *resetBtn = [UIButton new];
resetBtn.frame = CGRectMake(cancelBtn.frame.origin.x+cancelBtn.bounds.size.width+10, 5, (popupView.bounds.size.width-40)/3, 30);
[resetBtn setTitle:#"Reset" forState:UIControlStateNormal];
[resetBtn setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[resetBtn setBackgroundColor:[UIColor darkGrayColor]];
[popupView addSubview:resetBtn];
UIButton *doneBtn = [UIButton new];
doneBtn.frame = CGRectMake(popupView.bounds.size.width-10-((popupView.bounds.size.width-40)/3), 5, (popupView.bounds.size.width-40)/3, 30);
[doneBtn setTitle:#"Done" forState:UIControlStateNormal];
[doneBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[doneBtn setBackgroundColor:[UIColor blackColor]];
[popupView addSubview:doneBtn];
cancelBtn.translatesAutoresizingMaskIntoConstraints = NO;
[cancelBtn addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:30]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeTop multiplier:1.0f constant:5.0f] ];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeLeft multiplier:1.0f constant:10.0f]];
resetBtn.translatesAutoresizingMaskIntoConstraints = NO;
[resetBtn addConstraint:[NSLayoutConstraint constraintWithItem:resetBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:30]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:resetBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeTop multiplier:1.0f constant:5.0f] ];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:resetBtn attribute:NSLayoutAttributeLeft multiplier:1.0f constant:10.0f]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:doneBtn attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:resetBtn attribute:NSLayoutAttributeRight multiplier:1.0f constant:-10.0f]];
doneBtn.translatesAutoresizingMaskIntoConstraints = NO;
[doneBtn addConstraint:[NSLayoutConstraint constraintWithItem:doneBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:30]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:doneBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeTop multiplier:1.0f constant:5.0f] ];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:doneBtn attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeRight multiplier:1.0f constant:-10.0f]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:resetBtn attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:resetBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:doneBtn attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
How to fix this ?
I want to achieve a popup like this.
enter image description here
use stackView instead of UIView there are lots of flexibility.
You don't have to think about child view's autolayout inside stackView.
Stack View has a bunch of features to satisfy your needs
Code:
-(void)createStackViewWithButton {
UIButton *doneBtn = [UIButton new];
[doneBtn setTitle:#"Done" forState:UIControlStateNormal];
[doneBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[doneBtn setBackgroundColor:[UIColor blackColor]];
[doneBtn.heightAnchor constraintEqualToConstant:40].active = true;
[doneBtn.widthAnchor constraintEqualToConstant:100].active = true;
UIButton *doneBtn2 = [UIButton new];
[doneBtn2 setTitle:#"Cancel" forState:UIControlStateNormal];
[doneBtn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[doneBtn2 setBackgroundColor:[UIColor greenColor]];
[doneBtn2.heightAnchor constraintEqualToConstant:40].active = true;
[doneBtn2.widthAnchor constraintEqualToConstant:100].active = true;
UIButton *doneBtn3 = [UIButton new];
[doneBtn3 setTitle:#"Edit" forState:UIControlStateNormal];
[doneBtn3 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[doneBtn3 setBackgroundColor:[UIColor blueColor]];
[doneBtn3.heightAnchor constraintEqualToConstant:40].active = true;
[doneBtn3.widthAnchor constraintEqualToConstant:100].active = true;
UIStackView *stackView = [[UIStackView alloc] init];
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.distribution = UIStackViewDistributionEqualSpacing;
stackView.alignment = UIStackViewAlignmentCenter;
stackView.spacing = 0;
[stackView addArrangedSubview:doneBtn];
[stackView addArrangedSubview:doneBtn2];
[stackView addArrangedSubview:doneBtn3];
stackView.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:stackView];
//Layout for Stack View
[stackView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = true;
[stackView.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor].active = true;
}
output:
Image 1
Image 2
They are appearing one over another.
SOLVED ! Fixed it.
UIButton *cancelBtn = [UIButton new];
cancelBtn.frame = CGRectMake(10, 5, (popupView.bounds.size.width-40)/3, 30);
[cancelBtn setTitle:#"Cancel" forState:UIControlStateNormal];
[cancelBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[cancelBtn setBackgroundColor:[UIColor lightGrayColor]];
[popupView addSubview:cancelBtn];
UIButton *resetBtn = [UIButton new];
resetBtn.frame = CGRectMake(cancelBtn.frame.origin.x+cancelBtn.bounds.size.width+10, 5, (popupView.bounds.size.width-40)/3, 30);
[resetBtn setTitle:#"Reset" forState:UIControlStateNormal];
[resetBtn setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[resetBtn setBackgroundColor:[UIColor darkGrayColor]];
[popupView addSubview:resetBtn];
UIButton *doneBtn = [UIButton new];
doneBtn.frame = CGRectMake(resetBtn.frame.origin.x+resetBtn.bounds.size.width+10, 5, (popupView.bounds.size.width-40)/3, 30);
[doneBtn setTitle:#"Done" forState:UIControlStateNormal];
[doneBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[doneBtn setBackgroundColor:[UIColor blackColor]];
[popupView addSubview:doneBtn];
cancelBtn.translatesAutoresizingMaskIntoConstraints = NO;
[cancelBtn addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:30]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeTop multiplier:1.0f constant:5.0f] ];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f]];
resetBtn.translatesAutoresizingMaskIntoConstraints = NO;
[resetBtn addConstraint:[NSLayoutConstraint constraintWithItem:resetBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:30]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:resetBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeTop multiplier:1.0f constant:5.0f] ];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:resetBtn attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:cancelBtn attribute:NSLayoutAttributeRight multiplier:1.0f constant:10.0f]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:resetBtn attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:doneBtn attribute:NSLayoutAttributeLeft multiplier:1.0f constant:-10.0f]];
doneBtn.translatesAutoresizingMaskIntoConstraints = NO;
[doneBtn addConstraint:[NSLayoutConstraint constraintWithItem:doneBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:30]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:doneBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeTop multiplier:1.0f constant:5.0f] ];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:doneBtn attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f]];
// +++++++ Equal width constraints +++++++
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:resetBtn attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:resetBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:doneBtn attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0]];

Even after giving vertical contraints the individual views are overlapping

I am actually trying to have UIView which houses UIImageView and UILabel (side by side). There are multiple such pairs present. I need these pairs one below the other. But after the second pair, the overlapping starts in spite of giving vertical contraints. The code below is actually resulting in Views getting overlapped (third and fourth getting on to second) I am unable to know exactly what is going wrong. Appreciate if somebody could point out this
UIView *bottomCaseStudiesLyt = [[UIView alloc]init];
bottomCaseStudiesLyt.translatesAutoresizingMaskIntoConstraints = NO;
[contentView addSubview:bottomCaseStudiesLyt];
NSLayoutConstraint* bottomCaseStudiesleftConstraint = [NSLayoutConstraint constraintWithItem:bottomCaseStudiesLyt attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
[contentView addConstraint:bottomCaseStudiesleftConstraint];
NSLayoutConstraint* bottomCaseStudiesTopConstraint = [NSLayoutConstraint constraintWithItem:bottomCaseStudiesLyt attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:details3View attribute:NSLayoutAttributeBottom multiplier:1.0f constant:10.0f];
[contentView addConstraint:bottomCaseStudiesTopConstraint];
NSLayoutConstraint* bottomCaseStudiesRightConstraint = [NSLayoutConstraint constraintWithItem:bottomCaseStudiesLyt attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f];
[contentView addConstraint:bottomCaseStudiesRightConstraint];
NSLayoutConstraint* bottomCaseStudiesBottomConstraint = [NSLayoutConstraint constraintWithItem:bottomCaseStudiesLyt attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
[contentView addConstraint:bottomCaseStudiesBottomConstraint];
//NSArray *bottomCaseStudiesVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[headingDetailsView]-[details2View]-[details3View]-[bottomCaseStudiesLyt]|" options:0 metrics:nil views:#{#"headingDetailsView": headingDetailsView,#"details2View": details2View,#"details3View": details3View,#"bottomCaseStudiesLyt": bottomCaseStudiesLyt}];
//[contentView addConstraints:bottomCaseStudiesVConstraints];
UIView *firstView = [[UIView alloc]init];
[firstView setTranslatesAutoresizingMaskIntoConstraints:NO];
[bottomCaseStudiesLyt addSubview: firstView];
[self addImageAndDetails:bottomCaseStudiesLyt previousview:nil whichimage:#"ic_action_easy" whattext:#"Rediculously easy. Takes less than 30 seconds to build a room and go live" mynewview:firstView];
UIView *secondView = [[UIView alloc]init];
[secondView setTranslatesAutoresizingMaskIntoConstraints:NO];
[bottomCaseStudiesLyt addSubview: secondView];
[self addImageAndDetails:bottomCaseStudiesLyt previousview:firstView whichimage:#"ic_action_amaze" whattext:#"Engage members with great content, services, offers, polls, notification, quiz and more" mynewview:secondView];
UIView *thirdView = [[UIView alloc]init];
[thirdView setTranslatesAutoresizingMaskIntoConstraints:NO];
[bottomCaseStudiesLyt addSubview: thirdView];
[self addImageAndDetails:bottomCaseStudiesLyt previousview:secondView whichimage:#"ic_action_subscribers" whattext:#"Members ? No limit! There is a room for all. Go ahead and promote your room." mynewview:thirdView];
UIView *fourthView = [[UIView alloc]init];
[fourthView setTranslatesAutoresizingMaskIntoConstraints:NO];
[bottomCaseStudiesLyt addSubview: fourthView];
[self addImageAndDetails:bottomCaseStudiesLyt previousview:thirdView whichimage:#"ic_action_crossplatform" whattext:#"Your room can be accessed from any platform or device." mynewview:fourthView];
NSArray *bottomViewVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[firsView]-[secondView]-[thirdView]-[fourthView]|" options:0 metrics:nil views:#{#"firsView": firstView,#"secondView": secondView,#"thirdView": thirdView,#"fourthView": fourthView}];
[bottomCaseStudiesLyt addConstraints:bottomViewVConstraints];
// GetStarted button
self->m_ObjGetStartedBut = [[UIButton alloc]init];
[self->m_ObjGetStartedBut setTitle: [NSString stringWithFormat:#"Get Started"] forState:UIControlStateNormal];
self->m_ObjGetStartedBut.backgroundColor = [UIColor redColor];
[self->m_ObjGetStartedBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self->m_ObjGetStartedBut.translatesAutoresizingMaskIntoConstraints = NO;
self->m_ObjGetStartedBut.layer.cornerRadius = 10;
self->m_ObjGetStartedBut.clipsToBounds = YES;
[parentView addSubview:self->m_ObjGetStartedBut];
NSDictionary *myTopViews = #{
#"scrollView": self->myScrollView,
#"submitButton": self->m_ObjGetStartedBut
};
NSArray *myTopVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[scrollView]-[submitButton(40)]|" options:0 metrics:nil views:myTopViews];
NSArray *myTopHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[submitButton]-|" options:0 metrics:nil views:#{#"submitButton": self->m_ObjGetStartedBut}];
[parentView addConstraints:myTopVConstraints];
[parentView addConstraints:myTopHConstraints];
//[self->m_ObjGetStartedBut addTarget:self action:#selector(buttonIsReleased:) forControlEvents: UIControlEventTouchUpInside];
//[self->m_ObjGetStartedBut setTag:1];
}
-(UIView *)addImageAndDetails:(UIView *)localparentView previousview:(UIView *)prevView whichimage:(NSString *)imageName whattext:(NSString *)relatedText mynewview:(UIView *)itemView
{
NSLayoutConstraint* topViewleftConstraint = [NSLayoutConstraint constraintWithItem:itemView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:localparentView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
[localparentView addConstraint:topViewleftConstraint];
NSLayoutConstraint* topViewRightConstraint = [NSLayoutConstraint constraintWithItem:itemView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:localparentView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f];
[localparentView addConstraint:topViewRightConstraint];
NSLayoutConstraint* topViewTopConstraint = nil;
if(prevView == nil)
{
topViewTopConstraint = [NSLayoutConstraint constraintWithItem:itemView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:localparentView attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
[localparentView addConstraint:topViewTopConstraint];
}
else
{
//topViewTopConstraint = [NSLayoutConstraint constraintWithItem:itemView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:prevView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
//[localparentView addConstraint:topViewTopConstraint];
}
UIImage *easyToCreateImg = [UIImage imageNamed:imageName];
UIImageView *easyToCreateImgView = [[UIImageView alloc] initWithImage:easyToCreateImg];
easyToCreateImgView.contentMode = UIViewContentModeScaleToFill;
easyToCreateImgView.translatesAutoresizingMaskIntoConstraints = NO;
easyToCreateImgView.clipsToBounds = YES;
[itemView addSubview:easyToCreateImgView];
NSLayoutConstraint* easyToCreateImgLeftConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateImgView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:itemView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:2.0f];
[itemView addConstraint:easyToCreateImgLeftConstraint];
NSLayoutConstraint* easyToCreateImgTopConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateImgView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:itemView attribute:NSLayoutAttributeTop multiplier:1.0f constant:2.0f];
[itemView addConstraint:easyToCreateImgTopConstraint];
NSLayoutConstraint *easyToCreateImgHtConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateImgView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:80.0f];
[itemView addConstraint:easyToCreateImgHtConstraint];
NSLayoutConstraint *easyToCreateImgWidConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateImgView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:80.0f];
[itemView addConstraint:easyToCreateImgWidConstraint];
UILabel* easyToCreateLblView = [[UILabel alloc]init];
[easyToCreateLblView setText:relatedText];
easyToCreateLblView.numberOfLines = 0;
UIFont *newHeadingViewLblFont = [UIFont fontWithName:#"Arial" size:13];
[easyToCreateLblView setFont:newHeadingViewLblFont];
[easyToCreateLblView setTextColor:[UIColor blackColor]];
easyToCreateLblView.translatesAutoresizingMaskIntoConstraints = NO;
[itemView addSubview:easyToCreateLblView];
NSLayoutConstraint* easyToCreateLblTopConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateLblView attribute:NSLayoutAttributeCenterYWithinMargins relatedBy:NSLayoutRelationEqual toItem:easyToCreateImgView attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:2.0f];
[itemView addConstraint:easyToCreateLblTopConstraint];
NSLayoutConstraint* easyToCreateLblLeftConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateLblView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:easyToCreateImgView attribute:NSLayoutAttributeRight multiplier:1.0f constant:2.0f];
[itemView addConstraint:easyToCreateLblLeftConstraint];
NSLayoutConstraint* easyToCreateLblRightConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateLblView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:itemView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f];
[itemView addConstraint:easyToCreateLblRightConstraint];
return itemView;
}
I am not sure if my interpretation is right or wrong. What I observed is that when we give width and height constraints to the first view, this I think is not respected by the subsequent views that have been added. However, it does seem to respect the other constraints (top, bottom, leading, trailing). So when I started giving the same magnitude of width and height to the other subsequent views, then the arrangement came out as expected. Thoughts from the community are welcome.

changes subView frame when rotate

i add subView(backgroundView) in uiview in my base view controller its working well in potratate but when i change orientation from potraite to landscape its frame size is same as potrait i want to change size of subview when rotate.
UIView *activityView = [[UIView alloc] initWithFrame:self.view.bounds];
CGRect frame = activityView.frame;
activityView.frame = frame;
activityView.backgroundColor = [UIColor clearColor];
activityView.alpha = 0.0f;
[self.view addSubview:activityView];
self.activityView = activityView;
UIView *backgroundView = [[UIView alloc]initWithFrame:activityView.bounds];
backgroundView.alpha = 0.0f;
[backgroundView setBackgroundColor:[UIColor lightGrayColor]];
[self.activityView backgroundView];
UIActivityIndicatorView *spinning = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[self.activityView spinning];
spinning.center = activityView.center;
self.activityView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
[spinning setColor:[UIColor lightGrayColor]];
[spinning startAnimating];
To do this you must add constraints to your view (AutoLayout).
For example :
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0]];
Edit :
Don't forget to set translatesAutoresizingMaskIntoConstraints property to NO on views.
(Replace self.view and containerView with your views).

Need to set Auto layout constraint use code for my UIButton at bottom-centre

This is my updated question !
I have searched many tutorial & sites for setting Auto layout constraints to set my UIButton at bottom-centre of my view controller.I use code to create uiButton and i have set position,but typically i can see my uiButton are positioning in different place in different simulator(4S,5,6,INFACT ON MY OWN DEVICE).i need to set my uibutton at bottom-centre Like this image
I am new to ios ,so can't able to set constraint for my UIButton.And this is my UIButton code:
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]];
This above code is set for at top-right.so i have changed that to bottom,centre but i can't able to see my button.i need my button Like this image button position Not able to set constraint to place my uibutton position at in same place.Kindly any one can help me out to solve my problem
I have check your code.Replace this below code with your code .You can get your button at position as you required
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]];
Hope this helpful.Please learn some tutorial for auto layout.sure this concepts are very helpful for making App ...

NSLayoutConstraints to UITextField - Error

I have a simple setup with 3 views:
view, which has 1 subview contentView, which has 1 subview emailField (a UITextField).
emailfField is not aligning in centerY correctly. I got is this error:
"<NSLayoutConstraint:0x9e7d500 UIView:0x9e85c30.centerY == UITextField:0x9e4dbd0.centerY>",
"<NSLayoutConstraint:0x9e7cc00 UIView:0x9e85530.top == UIView:0x9e85c30.top>",
"<NSAutoresizingMaskLayoutConstraint:0x9e7f950 h=--& v=--& UITextField:0x9e4dbd0.midY == + 282.5>",
"<NSAutoresizingMaskLayoutConstraint:0x9e81e90 h=--& v=--& UIView:0x9e85c30.midY == + 240>")
My .m file:
- (void)viewDidLoad {
[super viewDidLoad];
[self initContentView];
[self initFirstView];
[self addConstraints];}
- (void)addConstraints {
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0]];
//First View
//Email Field
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.emailField
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];}
- (void)initContentView {
self.view.backgroundColor = [UIColor colorWithRed:239.0f/255.0f green:239.0f/255.0f blue:239.0f/255.0f alpha:1.0];
self.contentView = [[UIView alloc] initWithFrame:self.view.frame];
self.contentView.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.contentView];
int frameHorizontalCenter = self.contentView.frame.size.width/2;
self.funView = [[UIImageView alloc] initWithFrame:CGRectMake(frameHorizontalCenter-50, 100, 100, 100)];
self.funView.backgroundColor = [UIColor clearColor];
self.funView.image = [UIImage imageNamed:#"dog1"];
[self.contentView addSubview:self.funView];
self.descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(frameHorizontalCenter - 140, self.funView.frame.origin.y + self.funView.frame.size.height, 280, 60)];
self.descriptionLabel.font = [UIFont systemFontOfSize:14];
self.descriptionLabel.textColor = [UIColor colorWithRed:63.0f/255.0f green:63.0f/255.0f blue:63.0f/255.0f alpha:1.0];
self.descriptionLabel.backgroundColor = [UIColor clearColor];
self.descriptionLabel.highlightedTextColor = [UIColor whiteColor];
self.descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.descriptionLabel.numberOfLines = 2;
self.descriptionLabel.adjustsFontSizeToFitWidth = YES;
self.descriptionLabel.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:self.descriptionLabel];
self.emailField = [[UITextField alloc] initWithFrame:CGRectMake(frameHorizontalCenter -140, self.descriptionLabel.frame.origin.y + self.descriptionLabel.frame.size.height, 280, 45)];
self.emailField.borderStyle = UITextBorderStyleNone;
self.emailField.textColor = [UIColor colorWithRed:63.0f/255.0f green:63.0f/255.0f blue:63.0f/255.0f alpha:1.0];
self.emailField.backgroundColor = [UIColor whiteColor];
self.emailField.placeholder = #"Epasts";
[self.contentView addSubview:self.emailField];}
I really don't understand what's wrong. Aren't constraints set correctly?
Use setTranslatesAutoresizingMaskIntoConstraints: to turn off conversion of autoresizing masks to constraints on contentView and emailField. Add additional constraints to emailField to specify the x position, width and height that it should use.

Resources