NSString *format = [NSString stringWithFormat:@"H:|-10-[self]-10-|"];
NSString *formatVertical = [NSString stringWithFormat:@"V:|-40-[self(300)]|"];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary]];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:formatVertical options:0 metrics:nil views:viewsDictionary]];
with formatVertical i am able to set height of my view. But i want to make this value as dynamic to be able to show in all different screens as same look.
Is there anything i can achieve this?
Visual Format Language is nice, except there are a number of layout attributes it cannot do - such as height/width percentages and centering.
The answer from "K. Davydenko" will work... sort of. It still sets a fixed height for your subview though. So if you put that in the wrong place (before the superview is laid out, for example), or if the view changes (such as device rotation), you won't get what you're after.
Since it is rather simple to add a NSLayoutConstraint
in addition to your VFL formatting, that's the better option:
UIView *v = [UIView new];
v.translatesAutoresizingMaskIntoConstraints = NO;
v.backgroundColor = [UIColor blueColor];
[self.view addSubview:v];
NSDictionary *viewsDictionary = @{@"self":v};
NSString *format = [NSString stringWithFormat:@"H:|-10-[self]-10-|"];
// vertical formatting (40-pts from the top), but do NOT set a height
// (and do not include the trailing "|")
NSString *formatVertical = [NSString stringWithFormat:@"V:|-40-[self]"];
NSMutableArray *constraints = [NSMutableArray new];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary]];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:formatVertical options:0 metrics:nil views:viewsDictionary]];
// now add a constraint to set the Height of your subview equal to
// the height of the superview, with a 0.5 multiplier
// This will keep your view 1/2 the height of the superview, even
// when the superview's size changes
NSLayoutConstraint *c = [NSLayoutConstraint
constraintWithItem:v
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0.0];
[constraints addObject:c];
[self.view addConstraints:constraints];