I am creating view programmatically, In it there is two sub views, I have set height and width constraint for that,
what I want is like this,
UIView (variable height)
[10px gap]
UIView (fix height 40)
but I got:
My code is:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *button1, *button2 ;
button1=[UIView new];
button2=[UIView new];
button1.backgroundColor=[UIColor redColor];
button2.backgroundColor=[UIColor yellowColor];
button1.translatesAutoresizingMaskIntoConstraints=button2.translatesAutoresizingMaskIntoConstraints=NO;
[self.view addSubview:button1];
[self.view addSubview:button2];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(button1,button2);
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[button1]-|"
options:0
metrics:nil
views:viewsDictionary];
[self.view addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-[button1]-10-[button2]-|"
options: NSLayoutFormatAlignAllLeft
metrics:nil
views:viewsDictionary];
[self.view addConstraints:constraints];
}
Edit
Second Try
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *button1, *button2 ;
button1=[UIView new];
button2=[UIView new];
button1.backgroundColor=[UIColor redColor];
button2.backgroundColor=[UIColor yellowColor];
button1.translatesAutoresizingMaskIntoConstraints=button2.translatesAutoresizingMaskIntoConstraints=NO;
[self.view addSubview:button1];
[self.view addSubview:button2];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:button1
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:25.0]];
// [self.view addConstraint:[NSLayoutConstraint constraintWithItem:button1
// attribute:NSLayoutAttributeWidth
// relatedBy:NSLayoutRelationEqual
// toItem:nil
// attribute:NSLayoutAttributeNotAnAttribute
// multiplier:1.0
// constant:100]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:button1
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:-25.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:button1
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:30]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:button1
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:button2
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-30.0]];
//// Yellow
/// Left
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:button2
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:25.0]];
//Right
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:button2
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:-50.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:button2
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:button1
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:30]];
// [self.view addConstraint:[NSLayoutConstraint constraintWithItem:button2
// attribute:NSLayoutAttributeBottom
// relatedBy:NSLayoutRelationEqual
// toItem:self.view
// attribute:NSLayoutAttributeBottom
// multiplier:1.0
// constant:-30.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:button2
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:100]];
}
In this I got:
Regular views don't have any intrinsic size, and you've given the system no hints about how big the views should be, so button 1 gets laid out first with at least 10 points to spare, and view 2 ends up being 0 points high and 0 points wide.
To correct this, make sure that you give both views some horizontal rules, not just one of the views. Secondly, make sure you give the system some idea about height. If you want the views to be equal sizes, you need to tell the system that. Add another horizontal constraint for button 2:
constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[button2]-|" options:0 metrics:0 views:viewsDictionary];
[[self view] addConstraints:constraints];
Then add a height constraint for the view, in this case, adjust your vertical constraints to make the views equal heights by adding the (==button1) size information:
constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-[button1]-10-[button2(==button1)]-|"
options:0
metrics:nil
views:viewsDictionary];
Now you should see two views, red on top, yellow on bottom, that take up an equal amount of vertical space, have 10p space between and stretch to 20 points of each side of the container view.
To create these same constraints manually (which I don't recommend), you would do something like this:
UIView* view = [self view]; // for brevity
NSMutableArray* manualConstraints = [NSMutableArray array];
NSLayoutConstraint* b1_top = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTop multiplier:1 constant:20];
[manualConstraints addObject:b1_top];
NSLayoutConstraint* b1_left = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeLeading multiplier:1 constant:20];
[manualConstraints addObject:b1_left];
NSLayoutConstraint* b1_right = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTrailing multiplier:1 constant:-20];
[manualConstraints addObject:b1_right];
NSLayoutConstraint* b1_bottom = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:button2 attribute:NSLayoutAttributeTop multiplier:1 constant:-10];
[manualConstraints addObject:b1_bottom];
NSLayoutConstraint* b2_left = [NSLayoutConstraint constraintWithItem:button2 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeLeading multiplier:1 constant:20];
[manualConstraints addObject:b2_left];
NSLayoutConstraint* b2_right = [NSLayoutConstraint constraintWithItem:button2 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTrailing multiplier:1 constant:-20];
[manualConstraints addObject:b2_right];
NSLayoutConstraint* b2_bottom = [NSLayoutConstraint constraintWithItem:button2 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeBottom multiplier:1 constant:-20];
[manualConstraints addObject:b2_bottom];
NSLayoutConstraint* b2_height = [NSLayoutConstraint constraintWithItem:button2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:40];
[manualConstraints addObject:b2_height];
// Add all constraints
[view addConstraints:manualConstraints];
Related
Most of my scene is in storyboard and uses autolayout. But I want to create a UITableView, label and view in code and constrain them within the overall autlayout. My question is where in the lifecycle should I create the constraints.
Right now, I create hidden versions of the elements in ViewDidLoad and then customize and display them in View Will Appear based on data. I don't think I can put the layout constraints in ViewDidLoad, because the compiler won't know where all the views from storyboard are located. On the other hand, I don't want to recreate these constraints everytime viewWillAppear fires. Most don't change and at most I might want to update one or two.
Should I place the constraints in viewWillAppear and condition creating them on some test whether they were already created? Or should I put them somewhere else such as viewDidlayoutSubviews or viewDidAppear?
Thanks for any suggestions.
This is the code that creates the constraints:
NSLayoutConstraint *contop = [NSLayoutConstraint constraintWithItem:_stepsTableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_stepNames attribute:NSLayoutAttributeBottom multiplier:1 constant:12];
NSLayoutConstraint *contrail = [NSLayoutConstraint
constraintWithItem:_stepsTableView attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual toItem:self.scrollView
attribute:NSLayoutAttributeTrailing multiplier:1 constant:20];
NSLayoutConstraint *conlead = [NSLayoutConstraint
constraintWithItem:_stepsTableView attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual toItem:self.scrollView
attribute:NSLayoutAttributeLeading multiplier:1 constant:20];
NSLayoutConstraint *conbot = [NSLayoutConstraint
constraintWithItem:_stepsTableView attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual toItem:self.scrollView
attribute:NSLayoutAttributeBottom multiplier:1 constant:20];
NSLayoutConstraint *conheight = [NSLayoutConstraint constraintWithItem:_stepsTableView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:height];
[self.scrollView removeConstraint: _bottomConstraint];
[self.scrollView addConstraints:#[contop,contrail,conlead,conbot,conheight]];
[self.view layoutIfNeeded];
Any programmatically created constraints should be placed in viewDidLayoutSubviews and wraped with once bool value as the function is being called multiple times during launch of a viewController
-(void)viewDidLayoutSubviews
{
if(once){
once = NO;
_stepsTableView.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *contop = [NSLayoutConstraint constraintWithItem:_stepsTableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_stepNames attribute:NSLayoutAttributeBottom multiplier:1 constant:12];
NSLayoutConstraint *contrail = [NSLayoutConstraint
constraintWithItem:_stepsTableView attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual toItem:self.scrollView
attribute:NSLayoutAttributeTrailing multiplier:1 constant:20];
NSLayoutConstraint *conlead = [NSLayoutConstraint
constraintWithItem:_stepsTableView attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual toItem:self.scrollView
attribute:NSLayoutAttributeLeading multiplier:1 constant:20];
NSLayoutConstraint *conbot = [NSLayoutConstraint
constraintWithItem:_stepsTableView attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual toItem:self.scrollView
attribute:NSLayoutAttributeBottom multiplier:1 constant:20];
NSLayoutConstraint *conheight = [NSLayoutConstraint constraintWithItem:_stepsTableView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:height];
[self.scrollView removeConstraint: _bottomConstraint];
[self.scrollView addConstraints:#[contop,contrail,conlead,conbot,conheight]];
[self.view layoutIfNeeded];
}
}
I need to design the view like below image and I have tried with fixed heights and also tried compact width and regular height and regular width and compact height but those scenarios did not worked for me.
How can i set View height as percentage of the screen height in Storyboards?
I'm using Xcode 7
Basically you need to act on the multiplier property of an height equal constraint. To do that while pressing CTRL, drag from the view to its superview and select equal heights constraint, later edit this constraint in Size Inspector by setting its multiplier the desired value can be expressed also as 1:25, 1/25, 0,025. If it working on the contrary just reverse items as in the picture.
You can simply set the height of each one as a ratio of the UI height, 15% height would be;
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * 0.15)
Programmatic version
- (void)overallViewConstratints {
_firstView = [[UIView alloc]init];
[_firstView setTranslatesAutoresizingMaskIntoConstraints:NO];
_firstView.backgroundColor = [UIColor grayColor];
[self.view addSubview:_firstView];
NSLayoutConstraint *firstViewTop = [NSLayoutConstraint constraintWithItem:_firstView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0];
NSLayoutConstraint *firstViewLeading = [NSLayoutConstraint constraintWithItem:_firstView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0];
NSLayoutConstraint *firstViewTrailing = [NSLayoutConstraint constraintWithItem:_firstView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0];
NSLayoutConstraint *firstViewHeight = [NSLayoutConstraint constraintWithItem:_firstView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.15 constant:0.0];
NSArray *firstViewConstraints = #[firstViewTop, firstViewLeading, firstViewTrailing, firstViewHeight];
[self.view addConstraints:firstViewConstraints];
_secondView = [[UIView alloc]init];
_secondView.backgroundColor = [UIColor darkGrayColor];
[self.secondView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:_secondView];
NSLayoutConstraint *secondViewTop = [NSLayoutConstraint constraintWithItem:_secondView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_firstView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
NSLayoutConstraint *secondViewLeading = [NSLayoutConstraint constraintWithItem:_secondView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0];
NSLayoutConstraint *secondViewTrailing = [NSLayoutConstraint constraintWithItem:_secondView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0];
NSLayoutConstraint *secondViewHeight = [NSLayoutConstraint constraintWithItem:_secondView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.25 constant:0.0];
NSArray *secondViewConstraints = #[secondViewTop, secondViewLeading, secondViewTrailing, secondViewHeight];
[self.view addConstraints:secondViewConstraints];
_thirdView = [[UIView alloc]init];
_thirdView.backgroundColor = [UIColor lightGrayColor];
[_thirdView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:_thirdView];
NSLayoutConstraint *thirdViewTop = [NSLayoutConstraint constraintWithItem:_thirdView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_secondView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
NSLayoutConstraint *thirdViewLeading = [NSLayoutConstraint constraintWithItem:_thirdView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0];
NSLayoutConstraint *thirdViewTrailing = [NSLayoutConstraint constraintWithItem:_thirdView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0];
NSLayoutConstraint *thirdViewHeight = [NSLayoutConstraint constraintWithItem:_thirdView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.40 constant:0.0];
NSArray *thirdViewConstraints = #[thirdViewTop, thirdViewLeading, thirdViewTrailing, thirdViewHeight];
[self.view addConstraints:thirdViewConstraints];
_fourthView = [[UIView alloc]init];
_fourthView.backgroundColor = [UIColor brownColor];
[_fourthView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:_fourthView];
NSLayoutConstraint *fourthViewTop = [NSLayoutConstraint constraintWithItem:_fourthView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_thirdView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
NSLayoutConstraint *fourthViewLeading = [NSLayoutConstraint constraintWithItem:_fourthView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0];
NSLayoutConstraint *fourthViewTrailing = [NSLayoutConstraint constraintWithItem:_fourthView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0];
NSLayoutConstraint *fourthViewHeight = [NSLayoutConstraint constraintWithItem:_fourthView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.20 constant:0.0];
NSArray *fourthViewConstraints = #[fourthViewTop, fourthViewLeading, fourthViewTrailing, fourthViewHeight];
[self.view addConstraints:fourthViewConstraints];
}
I'm adding some views to my UIViewController in code and using NSLayoutConstraints to position them where I want them on screen. Being a little new to NSLayoutConstraints in code; I did a test setup and am 95% of the way there. This is my current code:
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
redView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:redView];
[redView addConstraint:[NSLayoutConstraint constraintWithItem:redView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil attribute:0
multiplier:1
constant:100]];
[redView addConstraint:[NSLayoutConstraint constraintWithItem:redView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1
constant:693]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1
constant:60]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
blueView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:blueView];
[blueView addConstraint:[NSLayoutConstraint constraintWithItem:blueView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:10
constant:50]];
[blueView addConstraint:[NSLayoutConstraint constraintWithItem:blueView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil attribute:0
multiplier:1
constant:693]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:blueView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:redView attribute:NSLayoutAttributeBottom
multiplier:1 constant:70]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:blueView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:redView
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
UIView *orangeView = [[UIView alloc] init];
orangeView.backgroundColor = [UIColor orangeColor];
orangeView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:orangeView];
[orangeView addConstraint:[NSLayoutConstraint constraintWithItem:orangeView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1
constant:25]];
[orangeView addConstraint:[NSLayoutConstraint constraintWithItem:orangeView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1
constant:693]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:orangeView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:blueView
attribute:NSLayoutAttributeBottom
multiplier:1
constant:25]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:orangeView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:blueView
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
Which looks like this - as I want it: (Ignore the grey background - thats the superView)
Now what I am trying to do is get the orangeViews height to fill the rest of the screen to the bottom of the superView. However when I add a bottom constraint to the orangeView it adjusts the other views heights, too.
How can I get the orange views height to fill the grey area below it?
Edit:
Removing the height constraint on the orangeView and adding a bottom constraint to the superview like this:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:orangeView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0]];
Changes the views like so:
You are setting a fixed height on the orange view, so how can it expand and fill the entire view ?
Also I suggest you pin the top of the view to the bottom of the previous view, since you can easily change view sizes and keep the spacing even, when you do so, else you will have to adjust your constants in the bottom to bottom relationship.
Here is a code that does work fine:
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
redView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:redView];
[redView addConstraint:[NSLayoutConstraint constraintWithItem:redView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1
constant:100]];
[redView addConstraint:[NSLayoutConstraint constraintWithItem:redView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1
constant:693]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1
constant:60]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
blueView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:blueView];
[blueView addConstraint:[NSLayoutConstraint constraintWithItem:blueView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1
constant:50]];
[blueView addConstraint:[NSLayoutConstraint constraintWithItem:blueView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil attribute:0
multiplier:1
constant:693]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:blueView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:redView attribute:NSLayoutAttributeBottom
multiplier:1 constant:10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:blueView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:redView
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
UIView *orangeView = [[UIView alloc] init];
orangeView.backgroundColor = [UIColor orangeColor];
orangeView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:orangeView];
[orangeView addConstraint:[NSLayoutConstraint constraintWithItem:orangeView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1
constant:693]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:orangeView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:blueView
attribute:NSLayoutAttributeBottom
multiplier:1
constant:25]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:orangeView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:orangeView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:blueView
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
What you need is to set Y constraints to orange view: set bottomLayout constraint and vertical spacing for blue view. If XCode warn you with orange warnings, read those warnings, it usually says something lile "you need X constraint for your view".
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"V:|-[yourViewHere(300)]-50-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(yourViewHere)]];
Where (300) is width of your view. That code snipper set constraint from bottomLayout to your view. Add similiar constraints as described above.
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];
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
}