Implement child UIView with fixed aspect ratio to aspect fit - ios

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:

Related

All the views are displayed on the top of the self.view behind the navigation bar when adding scroll view

I am new to ios. I recently tried scrollview. But all the subviews of the scroll view are displayed behind the navigation bar. I inspected in 3d view but placing a break point. I am calling this method from the viewDidLoad. Help
#import "AddContactViewController.h"
#import "NSStringCategory.h"
#import "ContactList.h"
#import "AddContactDelegate.h"
#import "ContactsListViewController.h"
#import "ContactDisplayingViewController.h"
#import "Contact.h"
#interface AddContactViewController ()
#property NSMutableArray * textFields;
#property UIDatePicker *inputViewDatePicker;
#property UIDatePicker *inputViewDatePicker1;
#property UIScrollView * scrollView;
#end
#implementation AddContactViewController
#pragma mark - Setting up the view
-(void)createView{
/*CGFloat x = 0;
CGFloat y = self.navigationController.navigationBar.frame.size.height;
CGFloat widthCG = self.view.frame.size.width;
CGFloat heightCG = self.view.frame.size.height;*/
NSLayoutConstraint * myConstraint;
//Date pickers
self.inputViewDatePicker = [[UIDatePicker alloc]init];
self.inputViewDatePicker.datePickerMode = UIDatePickerModeDate;
self.inputViewDatePicker1 = [[UIDatePicker alloc]init];
self.inputViewDatePicker1.datePickerMode = UIDatePickerModeDate;
[self.inputViewDatePicker addTarget:self action:#selector(datePickerChanged:) forControlEvents:UIControlEventValueChanged];
[self.inputViewDatePicker1 addTarget:self action:#selector(expiryDatePickerChanged:) forControlEvents:UIControlEventValueChanged];
self.scrollView = [[UIScrollView alloc]init];
//self.scrollView.frame = self.view.frame;
// self.scrollView.frame = CGRectMake(0, 0, 500, 500);
NSLog(#"The frame of the view is %#",self.view.frame);
// self.scrollView.contentSize = CGSizeMake(414,736);
[self.view addSubview:self.scrollView];
self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
myConstraint = [NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
[self.view addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:0];
[self.view addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0];
[self.view addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
[self.view addConstraint:myConstraint];
UILabel *name = [[UILabel alloc]init];
[name setText:#"Name"];
name.translatesAutoresizingMaskIntoConstraints = NO;
[self.scrollView addSubview:name];
self.nameTextField = [[UITextField alloc]init];
self.nameTextField.placeholder = #"Name";
myConstraint =[NSLayoutConstraint constraintWithItem:name
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeBaseline
multiplier:((float)2/10)
constant:0];
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:name attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeft multiplier:1 constant:10];
[self.scrollView addConstraint:myConstraint];
self.nameTextField.translatesAutoresizingMaskIntoConstraints = NO;
self.nameTextField.borderStyle = UITextBorderStyleLine;
NSLog(#"%#",[self.nameTextField class]);
[self.scrollView addSubview:self.nameTextField];
myConstraint =[NSLayoutConstraint constraintWithItem:self.nameTextField
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeBaseline
multiplier:((float)2/10)
constant:0];
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.nameTextField
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeRight
multiplier:1
constant:-10];
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.nameTextField
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeWidth
multiplier:((float)0.65)
constant:0];
[self.scrollView addConstraint:myConstraint];
//Second Name
UILabel *secondName = [[UILabel alloc]init];
[secondName setText:#"Second Name"];
[secondName sizeToFit];
[self.scrollView addSubview:secondName];
self.secondNameTextField = [[UITextField alloc]init];
self.secondNameTextField.placeholder = #"Second Name";
myConstraint =[NSLayoutConstraint constraintWithItem:secondName
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeBaseline
multiplier:((float)3.5/10)
constant:0];
secondName.translatesAutoresizingMaskIntoConstraints = NO;
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:secondName attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeft multiplier:1 constant:10];
myConstraint =[NSLayoutConstraint constraintWithItem:self.secondNameTextField
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeBaseline
multiplier:((float)3.5/10)
constant:0];
[self.scrollView addSubview:self.secondNameTextField];
[self.scrollView addConstraint:myConstraint];
self.secondNameTextField.translatesAutoresizingMaskIntoConstraints = NO;
self.secondNameTextField.borderStyle = UITextBorderStyleLine;
myConstraint = [NSLayoutConstraint constraintWithItem:self.secondNameTextField
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeRight
multiplier:1
constant:-10];
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.secondNameTextField
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeWidth
multiplier:((float)0.65)
constant:0];
[self.scrollView addConstraint:myConstraint];
//Email
self.emailTextField = [[UITextField alloc]init];
UILabel *email = [[UILabel alloc]init];
[email setText:#"Email"];
[self.scrollView addSubview:email];
myConstraint =[NSLayoutConstraint constraintWithItem:email
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:[email superview]
attribute:NSLayoutAttributeBaseline
multiplier:((float)5/10)
constant:0];
email.translatesAutoresizingMaskIntoConstraints = NO;
self.emailTextField.translatesAutoresizingMaskIntoConstraints = NO;
self.emailTextField.borderStyle = UITextBorderStyleLine;
self.emailTextField.placeholder = #"Email";
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:email attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeft multiplier:1 constant:10];
[self.scrollView addConstraint:myConstraint];
[self.scrollView addSubview:self.emailTextField];
myConstraint =[NSLayoutConstraint constraintWithItem:self.emailTextField
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeBaseline
multiplier:((float)5/10)
constant:0];
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.emailTextField attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeRight multiplier:1 constant:-10];
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.emailTextField attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeWidth multiplier:((float)0.65) constant:0];
[self.scrollView addConstraint:myConstraint];
//Number
UILabel * number = [[UILabel alloc]init];
number.translatesAutoresizingMaskIntoConstraints = NO;
[number setText:#"Number"];
[self.scrollView addSubview:number];
myConstraint =[NSLayoutConstraint constraintWithItem:number
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:[number superview]
attribute:NSLayoutAttributeBaseline
multiplier:((float)6.5/10)
constant:0];
self.numberTextField.translatesAutoresizingMaskIntoConstraints = NO;
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:number attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeft multiplier:1 constant:10];
[self.scrollView addConstraint:myConstraint];
self.numberTextField = [[UITextField alloc]init];
self.numberTextField.placeholder = #"Number";
self.numberTextField.translatesAutoresizingMaskIntoConstraints = NO;
self.numberTextField.borderStyle = UITextBorderStyleLine;
myConstraint =[NSLayoutConstraint constraintWithItem:self.numberTextField
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeBaseline
multiplier:((float)6.5/10)
constant:0];
[self.scrollView addSubview:self.numberTextField];
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.numberTextField attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeRight multiplier:1 constant:-10];
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.numberTextField attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeWidth multiplier:((float)0.65) constant:0];
[self.scrollView addConstraint:myConstraint];
//Date of Birth
NSDate *now = [[NSDate alloc]init];
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY:MM:dd"];
self.theDob = [[UITextField alloc]init];
self.theDob.translatesAutoresizingMaskIntoConstraints = NO;
self.theDob.borderStyle = UITextBorderStyleLine;
//[self.theDob addTarget:self action:#selector(DobTapped:) forControlEvents:UIControlEventAllTouchEvents];
self.theDob.userInteractionEnabled = true;
self.theDob.enabled = true;
self.theDob.inputView = self.inputViewDatePicker;
self.theDob.text = [dateFormatter stringFromDate:now];
NSLog(#"The date is %#",[dateFormatter stringFromDate:now]);
[self.scrollView addSubview: self.theDob];
myConstraint =[NSLayoutConstraint constraintWithItem:self.theDob
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeBaseline
multiplier:((float)8/10)
constant:0];
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.theDob attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeRight multiplier:1 constant:-10];
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.theDob attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeWidth multiplier:((float)0.65) constant:0];
[self.scrollView addConstraint:myConstraint];
UILabel *dob = [[UILabel alloc]init];
[dob setText:#"DOB"];
[self.scrollView addSubview:dob];
myConstraint =[NSLayoutConstraint constraintWithItem:dob
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:[dob superview]
attribute:NSLayoutAttributeBaseline
multiplier:((float)8/10)
constant:0];
dob.translatesAutoresizingMaskIntoConstraints = NO;
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:dob attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeft multiplier:1 constant:10];
[self.scrollView addConstraint:myConstraint];
//Expiry Date
self.expirydate = [[UITextField alloc]init];
self.expirydate.translatesAutoresizingMaskIntoConstraints = NO;
[self.scrollView addSubview: self.expirydate];
myConstraint =[NSLayoutConstraint constraintWithItem:self.expirydate
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeBaseline
multiplier:((float)9.5/10)
constant:0];
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.expirydate attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeRight multiplier:1 constant:-10];
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.expirydate attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeWidth multiplier:((float)0.65) constant:0];
self.expirydate.text = [dateFormatter stringFromDate:now];
self.expirydate.userInteractionEnabled = true;
self.expirydate.enabled = true;
self.expirydate.borderStyle = UITextBorderStyleLine;
self.expirydate.text = [dateFormatter stringFromDate:[[NSDate alloc]init] ];
self.expirydate.inputView = self.inputViewDatePicker1;
[self.scrollView addConstraint:myConstraint];
UILabel *expiryDate = [[UILabel alloc]init];
[expiryDate setText:#"Expiry Date"];
[expiryDate sizeToFit];
[self.scrollView addSubview:expiryDate];
NSLog(#"%#",NSStringFromCGRect(self.scrollView.frame));
NSLog(#"%#",NSStringFromCGRect(self.nameTextField.frame));
NSLog(#"%# super class",[self.scrollView superview]);
NSLog(#"the scroll super class %#",[self.nameTextField superview]);
myConstraint =[NSLayoutConstraint constraintWithItem:expiryDate
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeBaseline
multiplier:((float)9.5/10)
constant:0];
expiryDate.translatesAutoresizingMaskIntoConstraints = NO;
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:expiryDate attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeft multiplier:1 constant:10];
[self.scrollView addConstraint:myConstraint];
//[self.scrollView addSubview:self.scrollView];
//Navigation Buttons
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:#selector(saveContact:)];
myConstraint = [NSLayoutConstraint constraintWithItem:self.nameTextField attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:name attribute:NSLayoutAttributeRight multiplier:1 constant:20];
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.emailTextField attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:email attribute:NSLayoutAttributeRight multiplier:1 constant:20];
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.secondNameTextField attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:secondName attribute:NSLayoutAttributeRight multiplier:1 constant:20];
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.numberTextField attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:number attribute:NSLayoutAttributeRight multiplier:1 constant:20];
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.theDob attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:dob attribute:NSLayoutAttributeRight multiplier:1 constant:20];
[self.scrollView addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:self.expirydate attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:expiryDate attribute:NSLayoutAttributeRight multiplier:1 constant:20];
[self.scrollView addConstraint:myConstraint];
}
Part of your problem - you are setting constraints on objects relative to the NSLayoutAttributeBaseline of the Scroll View... which is, I believe, always 0.
I would suggest you try to get the first label to show up where you want it. After that, add one element at a time, using constraints to position them relative to that first label.
Try this for your first constraint:
myConstraint =[NSLayoutConstraint constraintWithItem:name
attribute:NSLayoutAttributeBaseline
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeTop
multiplier:1
constant:20];
That should put the Baseline of your "Name" label at 20 pts from the top of the Scroll View.
You can try the following
[self.navigationController.navigationBar setTranslucent:NO];
This way, none of the subviews of your self.view will be behind the UINavigationBar.
Please let me know if it doesn't help or you're looking for something else.

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.

How to add UIbutton on UIview programmatically in ios using autolayouts

i want to add uibutton on UIview programmatically using autolayouts and tried some code but it's not working please help me some one
//Adding UIview using autolayouts
UIView * myView;
myView = [UIView new];
myView.translatesAutoresizingMaskIntoConstraints = NO;
myView.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
[self.view addSubview:myView];
NSLayoutConstraint * constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:30.0f];
[self.view addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:100.0f];
[self.view addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:100.0f];
[self.view addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:100.0f];
[self.view addConstraint:constraint];
Adding button on uiview using autolayouts:-
mainButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[mainButton setTitle:#"MainButton" forState:UIControlStateNormal];
[mainButton sizeToFit];
mainButton.backgroundColor = [UIColor blackColor];
mainButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:mainButton];
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myview attribute:NSLayoutAttributeLeading multiplier:1.0f constant:30.f];
[self.view addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myview attribute:NSLayoutAttributeTop multiplier:1.0f constant:30.f];
[self.view addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:20.f];
[self.view addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:20.f];
[self.view addConstraint:constraint];
But button is not adding on myview please help me some one
This is working :
UIView *myView = [UIView new];
myView.translatesAutoresizingMaskIntoConstraints = NO;
myView.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
[self.view addSubview:myView];
NSLayoutConstraint * constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:30.0f];
[self.view addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:100.0f];
[self.view addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:140.0f];
[self.view addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:100.0f];
[self.view addConstraint:constraint];
UIButton *mainButton = [UIButton new];
mainButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[mainButton setTitle:#"MainButton" forState:UIControlStateNormal];
[mainButton sizeToFit];
mainButton.backgroundColor = [UIColor blackColor];
mainButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:mainButton];
NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:30.f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeTop multiplier:1.0f constant:30.f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:20.f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:mainButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:20.f];
[self.view addConstraint:constraint1];
You have not defined height constraint for button, you have define width constraint twice.

How to fix text fields and buttons programatically in all iphone inches using with autolayouts

Hi i am a beginner in iOS and in my project i have to apply auto-layouts programatically but here all fields width is not suitable for all iPhone inches in iPhone 4#5 inches it's ok but i phone 6 inch width is not suitable for all fields as like 4#5 and according to my code screen is coming like first image but i want to fix that all fields as like second screen i mean all fields have to suitable for all i phone inches please help me some-one and my code is below
myView = [UIview new];
myview .translatesAutoresizingMaskIntoConstraints = NO;
myview .backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
[self.view myView ];
textfield1= [UITextField new];
textfield1.translatesAutoresizingMaskIntoConstraints = NO;
textfield1.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
[self.view textfield1];
textfield2= [UITextField new];
textfield2.translatesAutoresizingMaskIntoConstraints = NO;
textfield2.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
[self.view textfield2];
textfield3= [UITextField new];
textfield3.translatesAutoresizingMaskIntoConstraints = NO;
textfield3.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
[self.view textfield3];
pickbutton = [UIButton new];
pickbutton .translatesAutoresizingMaskIntoConstraints = NO;
pickbutton .backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
[self.view pickbutton];
Submit= [UIButton new];
Submit.translatesAutoresizingMaskIntoConstraints = NO;
Submit.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
[self.view Submit];
Clear= [Clearnew];
Clear.translatesAutoresizingMaskIntoConstraints = NO;
Clear.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
[self.view Clear];
//Applying autolayouts for MyView
NSLayoutConstraint * constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
[self.view addConstraint:constraint];
constraint1 = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:10.0f];
[self.view addConstraint:constraint];
constraint1 = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: self.view attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:300.0f];
[self.view addConstraint:constraint];
constraint1 = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:460.0f];
[self.view addConstraint:constraint];
//Applying autolayouts for textfield1
NSLayoutConstraint * constraint1 = [NSLayoutConstraint constraintWithItem:textfield1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:textField1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeTop multiplier:1.0f constant:30.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:textField1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:300.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:textField1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
[self.view addConstraint:constraint1];
//Applying autolayouts for textfield2
NSLayoutConstraint * constraint2 = [NSLayoutConstraint constraintWithItem:textfield2 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
[self.view addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:textField2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeTop multiplier:1.0f constant:60.0f];
[self.view addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:textField2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:300.0f];
[self.view addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:textField2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
[self.view addConstraint:constraint2];
NSLayoutConstraint * constraint2 = [NSLayoutConstraint constraintWithItem:textfield2 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
[self.view addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:textField2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeTop multiplier:1.0f constant:60.0f];
[self.view addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:textField2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:300.0f];
[self.view addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:textField2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
[self.view addConstraint:constraint2];
//Applying auto layouts for textfield3
NSLayoutConstraint * constraint11 = [NSLayoutConstraint constraintWithItem:textfield3 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
[self.view addConstraint:constraint11];
constraint11 = [NSLayoutConstraint constraintWithItem:textField3 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeTop multiplier:1.0f constant:80.0f];
[self.view addConstraint:constraint11];
constraint11 = [NSLayoutConstraint constraintWithItem:textField3 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:300.0f];
[self.view addConstraint:constraint11];
constraint11 = [NSLayoutConstraint constraintWithItem:textField3 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
[self.view addConstraint:constraint11];
//Applying autolayouts for pickerbutton
NSLayoutConstraint * constraint22 = [NSLayoutConstraint constraintWithItem:pickerbutton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:100.0f];
[self.view addConstraint:constraint22];
constraint22 = [NSLayoutConstraint constraintWithItem:pickerbutton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeTop multiplier:1.0f constant:80.0f];
[self.view addConstraint:constraint22];
constraint22 = [NSLayoutConstraint constraintWithItem:pickerbutton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:80.0f];
[self.view addConstraint:constraint22];
constraint22 = [NSLayoutConstraint constraintWithItem:pickerbutton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
[self.view addConstraint:constraint22];
//Applying autolayouts for clearButton
NSLayoutConstraint * constraint4 = [NSLayoutConstraint constraintWithItem:Clear attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:207.0f];
[self.view addConstraint:constraint4];
constraint4 = [NSLayoutConstraint constraintWithItem:Clear attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeTop multiplier:1.0f constant:100.0f];
[self.view addConstraint:constraint4];
constraint4 = [NSLayoutConstraint constraintWithItem:Clear attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:133.0f];
[self.view addConstraint:constraint4];
constraint4 = [NSLayoutConstraint constraintWithItem:Clear attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
[self.view addConstraint:constraint4];
With "myView", you shouldn't set absolute size.
Here is example for your "myView":
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[self.view setBackgroundColor:[UIColor darkGrayColor]];
myView.translatesAutoresizingMaskIntoConstraints = NO;
[myView setBackgroundColor:[UIColor colorWithRed:74.0/255.0 green:166.0/255.0 blue:224.0/255.0 alpha:1]];
[self.view addSubview:myView];
NSDictionary *viewDic = #{#"myView": myView};
NSDictionary *metrics = #{#"vSpacing":#10, #"hSpacing":#10};
NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-vSpacing-[myView]-vSpacing-|"
options:0
metrics:metrics
views:viewDic];
NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-hSpacing-[myView]-hSpacing-|"
options:0
metrics:metrics
views:viewDic];
[self.view addConstraints:constraint_POS_V];
[self.view addConstraints:constraint_POS_H];
Result:

All images of UIScrollView are getting placed in the beginning

I am new to iOS programming. I want to generate all the controls using coding and then apply constraints to achieve autosize feature. I had achieved almost my requirement except for one problem and that is all images of my UIScrollView are getting placed at very beginning and rest of the UIScrollView stays empty. I think I am having some sort of problem with my constraints and currently I am not able to resolve it.
This is my code
self.bgView.image = [UIImage imageNamed:#"bg.png"];
NSDictionary *viewDictionary = #{#"bgImage":self.bgView,#"scrollView":self.scrollView};
NSDictionary *position = #{#"vSpacing":#0,#"hSpacing":#0};
//here I had specified the size of the background image corresponding to the view
NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-hSpacing-[bgImage]-hSpacing-|" options:0 metrics:position views:viewDictionary];
NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-vSpacing-[bgImage]-vSpacing-|" options:0 metrics:position views:viewDictionary];
[self.view addConstraints:constraint_POS_H];
[self.view addConstraints:constraint_POS_V];
//here I am specifying the size of scroll view
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.scrollView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.bgView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.scrollView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.bgView
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.scrollView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.bgView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.scrollView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.bgView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
//self.view.autoresizesSubviews = YES;
self.scrollView.pagingEnabled = YES;
NSInteger numberOfViews = photoArray.count;
for (int i=0; i < numberOfViews; i++) {
CGFloat myOrigin = i * self.view.frame.size.width;
NSLog(#"self.view.frame.size.width : %f",self.view.frame.size.width);
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, self.scrollView.frame.size.height)];
[myView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:myView];
//here I am specifying the size of uiview
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:myView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.scrollView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:myView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0.0]];
//here I am specifying the position of uiview
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:myView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:myView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
UIImageView *photos = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, myView.frame.size.width, self.scrollView.frame.size.height)];
//self.photos = [UIImageView new];
[photos setTranslatesAutoresizingMaskIntoConstraints:NO];
photos.image = [photoArray objectAtIndex:i];
[myView addSubview:photos];
//here I am specifying the size of image view within scroll view
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:photos
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:myView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:photos
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0.0]];
//here I am specifying the position of the image view
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:photos
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:photos
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:myView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
self.scrollView.delegate = self;
[self.scrollView addSubview:myView];
NSLog(#"self.myView.frame.size.width : %f",myView.frame.size.width);
}
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews,
self.scrollView.frame.size.height);
CGPoint scrollPoint = CGPointMake(0, 0);
[self.scrollView setContentOffset:scrollPoint animated:YES];
[self.view addSubview:self.scrollView];
you can easily solve it by just using reset to suggested constraints in storyboard.First select viewController and then press right bottom menu and select reset to suggested constraints in All views tab
This worked for me.

Resources