Width and Height Equal to Its Superview Using Autolayout Programmatically

Width and Height Equal to its superView using autolayout programmatically?

I'm not sure if this is the most efficient way to do it, but it works..

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.translatesAutoresizingMaskIntoConstraints = NO;
// initialize

[coverForScrolView addSubview:button];

NSLayoutConstraint *width =[NSLayoutConstraint
constraintWithItem:button
attribute:NSLayoutAttributeWidth
relatedBy:0
toItem:coverForScrolView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0];
NSLayoutConstraint *height =[NSLayoutConstraint
constraintWithItem:button
attribute:NSLayoutAttributeHeight
relatedBy:0
toItem:coverForScrolView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0];
NSLayoutConstraint *top = [NSLayoutConstraint
constraintWithItem:button
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:coverForScrolView
attribute:NSLayoutAttributeTop
multiplier:1.0f
constant:0.f];
NSLayoutConstraint *leading = [NSLayoutConstraint
constraintWithItem:button
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:coverForScrolView
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:0.f];
[coverForScrolView addConstraint:width];
[coverForScrolView addConstraint:height];
[coverForScrolView addConstraint:top];
[coverForScrolView addConstraint:leading];

How to set a view height to be equal to its width

Add the Aspect Ratio constraint to your viewSample Image

proportional height of view and superview autolayout

Set the view Equal Heights with its superview. Then change the multiplier of the Equal Heights constraint to 1:3

Sample Image

3 equal height views using autolayout programmatically

Have you tried using a UIStackView?

lazy var stackView: UIStackView = {
let stackView: UIStackView = UIStackView(arrangedSubviews: [self.firstView, self.secondView, self.thirdView])
stackView.axis = UILayoutConstraintAxis.vertical
stackView.distribution = UIStackViewDistribution.fillEqually
stackView.alignment = UIStackViewAlignment.fill
stackView.spacing = 10.0
return stackView
}()

Then use autolayout to make the height of one of the view's height. The stackView will take care of the other two views' height

Using auto layout constraints programmatically create four UIView all with equal height and width

Something like this:

/*
* ┌─┬─┐
* │1│2│
* ├─┼─┤
* │3│4│
* └─┴─┘
*/
override func viewDidLoad() {
super.viewDidLoad()
let view1 = UIView(frame: CGRectZero)
let view2 = UIView(frame: CGRectZero)
let view3 = UIView(frame: CGRectZero)
let view4 = UIView(frame: CGRectZero)

view1.backgroundColor = UIColor.yellowColor()
view2.backgroundColor = UIColor.redColor()
view3.backgroundColor = UIColor.greenColor()
view4.backgroundColor = UIColor.blueColor()

view1.setTranslatesAutoresizingMaskIntoConstraints(false)
view2.setTranslatesAutoresizingMaskIntoConstraints(false)
view3.setTranslatesAutoresizingMaskIntoConstraints(false)
view4.setTranslatesAutoresizingMaskIntoConstraints(false)

view.addSubview(view1)
view.addSubview(view2)
view.addSubview(view3)
view.addSubview(view4)

let views = ["view1":view1, "view2":view2, "view3":view3, "view4":view4]
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[view1][view2(==view1)]|", options: .allZeros, metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[view3][view4(==view3)]|", options: .allZeros, metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[view1][view3(==view1)]|", options: .allZeros, metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[view2][view4(==view2)]|", options: .allZeros, metrics: nil, views: views))

// Do any additional setup after loading the view, typically from a nib.
}

autolayout - make height of view relative to half superview height

This is now possible in IB as of [at least] Xcode 5.1.1. Although it took me sometime to figure out it is actually super simple:

First create a basic top alignment constraint (you will also need to setup bottom, left, and right constraints, like normal)
. Then select the constraint and navigate to the Attribute inspector:

Demonstration of steps above

Then you can adjust the multiplier. If you want it 50% of the super view leave it at 1, since it is aligned per the super's center. This is also a great way to create views that are other percentages too (like 25% of super view)

Demonstration of second step above



Related Topics



Leave a reply



Submit