How to set a constraint using percentage?

I demonstrate this below - you just have to change the value of multiplier. enter image description hereenter image description here

enter image description here


Update

Sorry, I have misunderstood your problem.

You'll need to add the constraints from code like so (the xConstraint is totally arbitrary, but you must need to define x, y positions, width and height, for an unambiguous layout):

@IBOutlet weak var imageView: UIImageView!

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        let yConstraint = NSLayoutConstraint(item: imageView, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: view.bounds.height / 3)

        let xConstraint = NSLayoutConstraint(item: imageView, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .Leading, multiplier: 1, constant: 30)

        NSLayoutConstraint.activateConstraints([yConstraint, xConstraint])
    }

This way, the equation will be:

imageView.top = 1 * view.top + (view.width / 3)

Original answer

Auto Layout uses the following equation for constraints:

aView.property = Multiplier * bView.property + Constant

Based on this, you can simply add an equal width/height constraint, then add a multiplier:

enter image description here

So the equation will be:

view.height = 0.3 * superView.height + 0

You should calculate it.

1. Calculate how many percents are from top to center ImageView

2. Set Vertical center to ImageView

3. Configure multiplier in Vertical center constraint and set multiplier from 1

For example: multiplier 0.5 will be 25% from top to center ImageView. So your multiplier will be ~0.6

By the way, there is another way how to do it:

1. Create transparent view from top to your imageView

2. Set height equal to your subview

3. Set multiplier to 0.3 to this height constraint

4. Set bottom space from your imageView to this transparent view equal to zero

Tags:

Ios

Autolayout