Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a constraint using percentage?

Tags:

Just a very direct question, but we had spent many hours trying to find a working solution but faild.

In Xcode, storyboard, how to set a constraint so one view can be located 30% of total window height from the top of the superview? And we need it to be that way for ALL supported iOS devices of all orientations.

Please see my illustration attached.enter image description here

like image 611
shawhu Avatar asked May 15 '15 13:05

shawhu


People also ask

How do you use percent constraint layout?

You can currently do this in a couple of ways. One is to create guidelines (right-click the design area, then click add vertical/horizontal guideline). You can then click the guideline's "header" to change the positioning to be percentage based. Finally, you can constrain views to guidelines.

What is a constraint layout?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread). As such, we are planning on enriching its API and capabilities over time.

WHAT IS barrier in constraint layout?

A Barrier references multiple widgets as input, and creates a virtual guideline based on the most extreme widget on the specified side. For example, a left barrier will align to the left of all the referenced views.


2 Answers

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

enter image description here

like image 123
L.Xing Avatar answered Feb 05 '23 02:02

L.Xing


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 
like image 43
József Vesza Avatar answered Feb 05 '23 00:02

József Vesza