How to add UIbutton on UIview programmatically in ios using autolayouts - ios

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.

Related

Adding imageview on UITextField with auto layout

I am creating a custom searchbar with UITextfield. Searchbar should look like this:
But, In my case it is appearing like this:
What is the mistake I am doing in setting constraints for imageview on textfield
Code:
UITextField *searchBarTF=[[UITextField alloc]init];
[searchBarTF setBackgroundColor:[UIColor whiteColor]];
searchBarTF.layer.borderColor=[[UIColor darkGrayColor]CGColor];
searchBarTF.layer.borderWidth=7;
searchBarTF.layer.cornerRadius=5;
searchBarTF.returnKeyType=UIReturnKeySearch;
searchBarTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Search by locations" attributes:#{NSForegroundColorAttributeName: [UIColor grayColor],NSFontAttributeName:[UIFont fontWithName:#"Helvetica" size:16]}];
searchBarTF.textAlignment=NSTextAlignmentCenter;
UIImageView *srchBariconImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"srchBarIcon"]];
[srchBariconImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
[searchBarTF addSubview:srchBariconImageView];
[searchBarTF addConstraint:[NSLayoutConstraint constraintWithItem:srchBariconImageView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:searchBarTF attribute:NSLayoutAttributeLeading multiplier:1.0f constant:40]];
[searchBarTF addConstraint:[NSLayoutConstraint constraintWithItem:srchBariconImageView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:searchBarTF attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-(views.frame.size.width-80)]];
[searchBarTF addConstraint:[NSLayoutConstraint constraintWithItem:srchBariconImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:40]];
//
// [searchBarTF addConstraint:[NSLayoutConstraint constraintWithItem:srchBariconImageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:40]];
[searchBarTF setTranslatesAutoresizingMaskIntoConstraints:NO];
// replace views with self.view.
[views addSubview:searchBarTF];
[views addConstraint:[NSLayoutConstraint constraintWithItem:searchBarTF attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:views attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[views addConstraint:[NSLayoutConstraint constraintWithItem:searchBarTF attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:60]];
// [views addConstraint:[NSLayoutConstraint constraintWithItem:searchBarTF attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:views attribute:NSLayoutAttributeTop multiplier:1.0 constant:-64]];
[views addConstraint:[NSLayoutConstraint constraintWithItem:searchBarTF attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:views attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
[views addConstraint:[NSLayoutConstraint constraintWithItem:searchBarTF attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:views attribute:NSLayoutAttributeTop multiplier:1.0 constant:130]];
Subclass UITextField and overwrite these methods
- (void)awakeFromNib{
CGRect frame = self.leftView.frame;
frame.size.width = 30;
frame.size.height = 30;
self.leftView.frame = frame;
self.leftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"underfloor_cross_blue.png"]];
}
- (CGRect)leftViewRectForBounds:(CGRect)bounds{
CGRect rect=CGRectMake(8, 8,50.0, 45);
[super leftViewRectForBounds:rect];
return rect ;
}
Note: change image and frame according to your need.

How to add container view on scrollview using constraint with item formate

I am new for auto-layouts and i am trying to add one container view on ScrollView using auto-layouts "constraint with item" formate but I can't adding using my code.
I know how to add using visual formate but I want to do this process using constraint with item formate. Please help me!
My code:
#import "ViewController3.h"
#interface ViewController3 ()
{
UIScrollView * scrollView;
UIView * containerView;
}
#end
#implementation ViewController3
- (void)viewDidLoad {
[super viewDidLoad];
scrollView = [[UIScrollView alloc] init];
scrollView.backgroundColor = [UIColor lightGrayColor];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:scrollView];
containerView = [[UIView alloc] init];
containerView.backgroundColor = [UIColor orangeColor];
containerView.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:containerView];
//Applying autolayouts for scrollview
NSLayoutConstraint * constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
//Applying autolayouts for containerview
NSLayoutConstraint * constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: scrollView attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
[scrollView addConstraint:constraint2];
}
With Autolayout, you don't need to set scrollview's contentSize explicitly, but you have to provide enough clues to let Autolayout system infer what exactly your scrollview's contentSize is.
There are generally two ways to layout UIScrollView with Autolayout, mixed or pure Autolayout.Check this:https://developer.apple.com/library/ios/technotes/tn2154/_index.html.
If it is pure Autolayout, you can achieve that by your containerView's subviews, that is, your subviews do not rely on the scroll view to get their size. Or you can just tie your containerView's width and height to any view outside your scrollview, like self.view, or to a fixed value.
E.g,
You can add four more lines :
constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
[self.view addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
[self.view addConstraint:constraint2];

How to scroll the scroll view using auto-layouts "constraint with item" formate in ios?

Here I am adding one scrollview programmatically on Viewcontroller using "constraint with item" format and inside that scrollview I have added one UIlabel. So everything is good.
But here I have given "NSLayoutAttributeBottom" between scrollview and label is "-200" that means scrollview is scrolling up to "200" range on viewcontroller controller, but according to my code it is not scrolling.
As per drag and drop procedure it's ok, I have checked, it's working fine.
Why it's not possible to do it programmatically?
my code is below:-
#import "ViewController4.h"
#interface ViewController4 ()
{
UIScrollView * scrollView;
UILabel * label;
}
#end
#implementation ViewController4
- (void)viewDidLoad {
[super viewDidLoad];
scrollView = [[UIScrollView alloc] init];
scrollView.backgroundColor = [UIColor lightGrayColor];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:scrollView];
label = [[UILabel alloc] init];
label.backgroundColor = [UIColor redColor];
label.text = #"Hello World1";
label.textAlignment = NSTextAlignmentCenter;
label.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:label];
//Applying autolayouts for scrollview
NSLayoutConstraint * constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
//Applying autolayouts for label
NSLayoutConstraint * constraint2 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: scrollView attribute:NSLayoutAttributeTop multiplier:1.0f constant:100.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:-100.0f];
[scrollView addConstraint:constraint2];
}
In above code i have clearly given NSLayoutAttributeBottom space is "-200" but scroollview is not scrolling what did i do here wrong please help me some and how can i scroll this scrollview

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:

Expand views on changing orientation and screen size in auto layout

I have a UIImageView which I need to expand (height and width) on changing orientation and screen size. I am using auto layout constraints for that.
topImageView.contentMode = UIViewContentModeScaleAspectFit;
topImageView.backgroundColor = [UIColor clearColor];
topImageView.layer.cornerRadius = 5.0f;
topImageView.clipsToBounds = YES;
topImageView.translatesAutoresizingMaskIntoConstraints = NO;
if(login_DO.logoPath)
[topImageView loadImage:login_DO.logoPath];
[self.view addSubview:topImageView];
NSArray *horizontalConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"H:|-(%i)-[topImageView(%f)]",X_OFFSET,VIEW_FRAME_WIDTH-X_OFFSET*2]
options:0 metrics:nil views:#{#"topImageView": topImageView}];
NSArray *verticalConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"V:|-(%f)-[topImageView(80)]",navHeight]
options:0 metrics:nil views:#{#"topImageView": topImageView}];
[self.view addConstraints:horizontalConstraints];
[self.view addConstraints:verticalConstraints];
NSLayoutConstraint *leadingMarginForImageConstraint = [NSLayoutConstraint
constraintWithItem:topImageView attribute:NSLayoutAttributeLeadingMargin
relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.view attribute:
NSLayoutAttributeLeadingMargin multiplier:1.0 constant:X_OFFSET];
NSLayoutConstraint *topMarginForImageConstraint = [NSLayoutConstraint
constraintWithItem:topImageView attribute:NSLayoutAttributeTopMargin
relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.view attribute:
NSLayoutAttributeTopMargin multiplier:1.0 constant:VIEW_FRAME_WIDTH-X_OFFSET*2];
[self.view addConstraints:#[ leadingMarginForImageConstraint,
topMarginForImageConstraint]];
But the image is not expanding. I am new to auto layouts. Am I missing any constraint?
you can change the imageBottomConstraint from -navHeight to some other value from bottom.
avoid using VIEW_FRAME_WIDTH cause it change when you change orientation.
UIView *superview = self.view;
NSLayoutConstraint *imageTopConstraint = [NSLayoutConstraint
constraintWithItem:topImageView attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual toItem:superview
attribute:NSLayoutAttributeTop multiplier:1.0 constant:navHeight];
NSLayoutConstraint *imageBottomConstraint = [NSLayoutConstraint
constraintWithItem:topImageView attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual toItem:superview
attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-navHeight];
NSLayoutConstraint *imageLeftConstraint = [NSLayoutConstraint
constraintWithItem:topImageView attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual toItem:superview attribute:
NSLayoutAttributeLeft multiplier:1.0 constant:X_OFFSET];
NSLayoutConstraint *imageRightConstraint = [NSLayoutConstraint
constraintWithItem:topImageView attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual toItem:superview attribute:
NSLayoutAttributeRight multiplier:1.0 constant:-X_OFFSET];
[superview addConstraints:#[imageBottomConstraint ,
imageLeftConstraint, imageRightConstraint,
imageTopConstraint]];
for more help check http://www.tutorialspoint.com/ios/ios_auto_layouts.htm
or try using https://github.com/marcoarment/CompactConstraint
let me know if it helped.
I tested the following code that adds an ImageView with Globe.png and add constraints so it appears as you describe. The difference is just to pinch all side edges to the superview (self.view) and then assign the constraints to the superview:
-(void)addImageView{
topImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Globe.png"]]; // Added test image
topImageView.contentMode = UIViewContentModeScaleAspectFit;
topImageView.backgroundColor = [UIColor clearColor];
topImageView.layer.cornerRadius = 5.0f;
topImageView.clipsToBounds = YES;
topImageView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:topImageView];
NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:topImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:topImageView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:topImageView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:topImageView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
[self.view addConstraints:#[topConstraint, bottomConstraint, leftConstraint, rightConstraint]]; //Note constraints are added to the superView
}

Resources