UITextfield becomes non editable when applying a few constraints - ios

Something strange is happening when I have UITextfield, UILabel and UIButton. I am applying constraints so that the label and the textfield are centred in the UIView. The button is pinned at the bottom. When I run this code, UITextField becomes non-editable. Can somebody tell me what is going wrong with this code below?
Click here to view the code output
UIView* topLayout = [[UIView alloc]init];
[topLayout setTranslatesAutoresizingMaskIntoConstraints:NO];
topLayout.backgroundColor = [UIColor whiteColor];
[self.view addSubview: topLayout];
NSArray *topLayoutHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:[parentView(==mainView)]" options:0 metrics:0 views:#{#"parentView": topLayout, #"mainView":self.view}];
NSArray *topLayoutVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[parentView]|" options:0 metrics:0 views:#{#"parentView": topLayout}];
[self.view addConstraints:topLayoutHConstraints];
[self.view addConstraints:topLayoutVConstraints];
UIView *childViewLayout = [[UIView alloc]init];
childViewLayout.backgroundColor = [UIColor yellowColor];
[childViewLayout setTranslatesAutoresizingMaskIntoConstraints:NO];
[topLayout addSubview:childViewLayout];
NSLayoutConstraint *childViewControlsHConstraints = [NSLayoutConstraint constraintWithItem:childViewLayout attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:topLayout attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
NSLayoutConstraint *childViewControlsVConstraints = [NSLayoutConstraint constraintWithItem:childViewLayout attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:topLayout attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];
NSLayoutConstraint *childViewControlsHtConstraints = [NSLayoutConstraint constraintWithItem:childViewLayout attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:topLayout attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0];
[topLayout addConstraint:childViewControlsHConstraints];
[topLayout addConstraint:childViewControlsVConstraints];
//[topLayout addConstraint:childViewControlsHtConstraints];
// Instruction field
UILabel* instructionFieldLbl = [[UILabel alloc]init];
[childViewLayout addSubview:instructionFieldLbl];
[instructionFieldLbl setText:#"Please check your SMS or E-Mail for the 4 digit actiation code. Please enter the same to activat your account"];
UIFont *instructionFieldLblFont = [UIFont fontWithName:#"Arial-BoldMT" size:13];
[instructionFieldLbl setFont:instructionFieldLblFont];
instructionFieldLbl.numberOfLines = 0;
[instructionFieldLbl sizeToFit];
[instructionFieldLbl setTextColor:[UIColor grayColor]];
instructionFieldLbl.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *instructionFieldHConstraint = [NSLayoutConstraint constraintWithItem:instructionFieldLbl attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationLessThanOrEqual toItem:childViewLayout attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0f];
//NSArray *instructionFieldHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-10-[instructionFieldLbl]-10-|" options:0 metrics:nil views:#{#"instructionFieldLbl": instructionFieldLbl}];
[childViewLayout addConstraint:instructionFieldHConstraint];
//[childViewLayout addConstraints:instructionFieldHConstraints];
dispatch_async(dispatch_get_main_queue(), ^{
instructionFieldLbl.preferredMaxLayoutWidth = self.view.bounds.size.width;
});
// Activation field
self->m_ObjActivationField = [[JVFloatLabeledTextField alloc]init];
[childViewLayout addSubview:self->m_ObjActivationField];
self->m_ObjActivationField.borderStyle = UITextBorderStyleRoundedRect;
self->m_ObjActivationField.translatesAutoresizingMaskIntoConstraints = NO;
self->m_ObjActivationField.placeholder = [NSString stringWithFormat:#"Enter the activtion code"];
NSLayoutConstraint *activationHorzConstraint = [NSLayoutConstraint constraintWithItem:self->m_ObjActivationField attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:childViewLayout attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
[childViewLayout addConstraint:activationHorzConstraint];
NSDictionary *myControlViews = #{
#"instructionFieldLbl": instructionFieldLbl,
#"activationField": self->m_ObjActivationField
};
NSArray *myControlViewsVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[instructionFieldLbl]-10-[activationField]|" options:0 metrics:nil views:myControlViews];
[childViewLayout addConstraints:myControlViewsVConstraints];
// Submit button
self->m_ObjActivationBut = [[UIButton alloc]init];
[topLayout addSubview:self->m_ObjActivationBut];
[self->m_ObjActivationBut setTitle: [NSString stringWithFormat:#"Activate"] forState:UIControlStateNormal];
self->m_ObjActivationBut.backgroundColor = [UIColor redColor];
[self->m_ObjActivationBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self->m_ObjActivationBut.translatesAutoresizingMaskIntoConstraints = NO;
self->m_ObjActivationBut.layer.cornerRadius = 10;
self->m_ObjActivationBut.clipsToBounds = YES;
NSDictionary *topViews = #{
#"childViewLayout": childViewLayout,
#"activationButton": self->m_ObjActivationBut
};
//NSArray *myTopVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[activationButton(40)]-10-|" options:0 metrics:nil views:topViews];
NSArray *myTopHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[activationButton]-|" options:0 metrics:nil views:#{#"activationButton": self->m_ObjActivationBut}];
NSLayoutConstraint *myTopVConstraints = [NSLayoutConstraint constraintWithItem:self->m_ObjActivationBut attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:topLayout attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10.0f];
[topLayout addConstraint:myTopVConstraints];
[topLayout addConstraints:myTopHConstraints];

Related

How to use constraints Programmatically? [closed]

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.

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.

adjustsFontSizeToFitWidth not showing the complete label with visual auto layout

I'm trying to make with auto layout a little notification box with two labels, a progress bar, and an X to cancel. I have the labels set up the same with font scaling, but it looks like some of the labels are getting cut off. I'm not sure if it's a result of my auto layout code or my UILabel code.
You can see that the text isn't aligned at all and the Playing in label is cut off completely.
Relevant code:
// Container
pttProgressContainer = [UIView new];
[pttProgressContainer setBackgroundColor:UIColorFromRGBWithAlpha(0x3a4362, 0.10)];
[pttProgressContainer setTranslatesAutoresizingMaskIntoConstraints:NO];
[pttContainer addSubview:pttProgressContainer];
// Separator
UIView *progressContainer1pxView = [UIView new];
[progressContainer1pxView setBackgroundColor:[UIColor whiteColor]];
[progressContainer1pxView setTranslatesAutoresizingMaskIntoConstraints:NO];
[pttProgressContainer addSubview:progressContainer1pxView];
// Progress viewzlers
UIView *progressViewLeft = [UIView new];
[progressViewLeft setTranslatesAutoresizingMaskIntoConstraints:NO];
[pttProgressContainer addSubview:progressViewLeft];
UIView *progressViewRight = [UIView new];
[progressViewRight setTranslatesAutoresizingMaskIntoConstraints:NO];
[pttProgressContainer addSubview:progressViewRight];
// Progress view
pttProgressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
[pttProgressView setTranslatesAutoresizingMaskIntoConstraints:NO];
[pttProgressView setProgress:0.5 animated:NO];
[pttProgressView setTrackTintColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.20]];
[pttProgressView setProgressTintColor:UIColorFromRGB(0xd7d7d7)];
[[pttProgressView layer] setCornerRadius:5];
[[pttProgressView layer] setMasksToBounds:YES];
[pttProgressView setClipsToBounds:YES];
[progressViewLeft addSubview:pttProgressView];
// Now the playing in label countdown
UILabel *pttPlayingInLabel = [UILabel new];
[pttPlayingInLabel setFont:[UIFont systemFontOfSize:20]];
[pttPlayingInLabel setAdjustsFontSizeToFitWidth:YES];
[pttPlayingInLabel setMinimumScaleFactor:8/20];
[pttPlayingInLabel setTextAlignment:NSTextAlignmentLeft];
[pttPlayingInLabel setTextColor:[UIColor whiteColor]];
[pttPlayingInLabel setContentMode:UIViewContentModeCenter];
[pttPlayingInLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[pttPlayingInLabel setText:#"Playing In..."];
[progressViewLeft addSubview:pttPlayingInLabel];
// Countdown numbers
pttPlayingCountdownLabel = [UILabel new];
[pttPlayingCountdownLabel setFont:[UIFont systemFontOfSize:20]];
[pttPlayingCountdownLabel setMinimumScaleFactor:8/20];
[pttPlayingCountdownLabel setAdjustsFontSizeToFitWidth:YES];
[pttPlayingCountdownLabel setTextAlignment:NSTextAlignmentRight];
[pttPlayingCountdownLabel setContentMode:UIViewContentModeCenter];
[pttPlayingCountdownLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[pttPlayingCountdownLabel setTextColor:[UIColor whiteColor]];
[pttPlayingCountdownLabel setText:#"5"];
[progressViewLeft addSubview:pttPlayingCountdownLabel];
// The button del cancel
pttCancelPlayingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[pttCancelPlayingButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[pttCancelPlayingButton setBackgroundImage:[UIImage imageNamed:#"ptt-button-delay-cancel.png"] forState:UIControlStateNormal];
// [pttCancelPlayingButton setBackgroundColor:[UIColor brownColor]];
// [pttCancelPlayingButton setTitle:#"C" forState:UIControlStateNormal];
[progressViewRight addSubview:pttCancelPlayingButton];
// Progress danker
[pttProgressContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[progressContainer1pxView(==1)]-[progressViewLeft]-|"
options:0
metrics:metrics
views:views]];
/*
[pttProgressContainer addConstraint:[NSLayoutConstraint constraintWithItem:progressViewRight
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:progressViewLeft
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0]];
[pttProgressContainer addConstraint:[NSLayoutConstraint constraintWithItem:progressViewRight
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:progressViewLeft
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0]];
*/
// Visual horizontal
[pttProgressContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[progressContainer1pxView]|"
options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom
metrics:metrics
views:views]];
[pttProgressContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|-[progressViewLeft]-[progressViewRight(==40)]-|"
options:NSLayoutFormatAlignAllBottom | NSLayoutFormatAlignAllTop
metrics:metrics
views:views]];
// Inner danker
[progressViewLeft addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-[pttPlayingInLabel]-[pttProgressView(==3)]-|"
options:NSLayoutFormatAlignAllLeading
metrics:metrics
views:views]];
/*
[progressViewLeft addConstraint:[NSLayoutConstraint constraintWithItem:pttPlayingCountdownLabel
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:pttPlayingInLabel
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0]];
[progressViewLeft addConstraint:[NSLayoutConstraint constraintWithItem:pttPlayingCountdownLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:pttPlayingInLabel
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0]];
*/
// Visual horizontal
[progressViewLeft addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|-[pttPlayingInLabel]-[pttPlayingCountdownLabel(==20)]-|"
options:NSLayoutFormatAlignAllBaseline
metrics:metrics
views:views]];
[progressViewLeft addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|-[pttProgressView]-|"
options:0
metrics:metrics
views:views]];
// Now for the inner danker right
[progressViewRight addConstraint:[NSLayoutConstraint constraintWithItem:pttCancelPlayingButton
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:21.0]];
[progressViewRight addConstraint:[NSLayoutConstraint constraintWithItem:pttCancelPlayingButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:21.0]];
[progressViewRight addConstraint:[NSLayoutConstraint constraintWithItem:pttCancelPlayingButton
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:progressViewRight
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
[progressViewRight addConstraint:[NSLayoutConstraint constraintWithItem:pttCancelPlayingButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:progressViewRight
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];

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.

Implement child UIView with fixed aspect ratio to aspect fit

I have some issue with autolayout.
We have ViewController with root view and child view.
Child view have fixed aspect ratio.
I need to fit child view in parent while rotation.
Also chid view should be centered.
Like on a picture:
I have this code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * v = [UIView new];
v.backgroundColor = [UIColor redColor];
v.translatesAutoresizingMaskIntoConstraints = NO;
v.tag = 100;
[self.view addSubview:v];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[v]|"
options:0
metrics:nil
views:#{#"v":v}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[v]|"
options:0
metrics:nil
views:#{#"v":v}]];
for (NSLayoutConstraint *c in self.view.constraints) {
[c setPriority:800];
}
NSLayoutConstraint *c = [NSLayoutConstraint constraintWithItem:v
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:v
attribute:NSLayoutAttributeWidth
multiplier:0.8
constant:0.];
[c setPriority:1000];
[v addConstraint:c];
c = [NSLayoutConstraint constraintWithItem:v
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0.];
[c setPriority:1000];
[self.view addConstraint:c];
c = [NSLayoutConstraint constraintWithItem:v
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0.];
[c setPriority:1000];
[self.view addConstraint:c];
}
And it's not working, it's shrinks outside superview in landscape.
I've made it work with the following constraints:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * v = [UIView new];
v.backgroundColor = [UIColor redColor];
v.translatesAutoresizingMaskIntoConstraints = NO;
v.tag = 100;
[self.view addSubview:v];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-(>=0)-[v]-(>=0)-|"
options:0
metrics:nil
views:#{#"v":v}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-(>=0)-[v]-(>=0)-|"
options:0
metrics:nil
views:#{#"v":v}]];
NSLayoutConstraint *c = [NSLayoutConstraint constraintWithItem:v
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0];
[c setPriority:800];
[self.view addConstraint:c];
c = [NSLayoutConstraint constraintWithItem:v
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0];
[c setPriority:800];
[self.view addConstraint:c];
c = [NSLayoutConstraint constraintWithItem:v
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:v
attribute:NSLayoutAttributeWidth
multiplier:0.8
constant:0.];
[c setPriority:1000];
[v addConstraint:c];
c = [NSLayoutConstraint constraintWithItem:v
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0.];
[c setPriority:1000];
[self.view addConstraint:c];
c = [NSLayoutConstraint constraintWithItem:v
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0.];
[c setPriority:1000];
[self.view addConstraint:c];
}
Screenshots:

Resources