I want to apply Auto Layout to UILabel like flexible height & width - ios

I applied flexible height & width to my UILabel and now I want to use that type of functionality by using Auto Layout.
I am able to increase height and width of UILabel as Device width and height, but X and Y is not changed according to Device width and Height.
Ex:-
Device :- iPhone 7
UILabel :-
X : 20
Y : 120
Width : 300
Height : 30
Device :- iPhone SE
UILabel :-
X : 20
Y : 120
Width : 245
Height : 25
Now problem is that Size of UILabel decrease with aspect ratio but X & Y coordinate was not changed.
According to flexible height & width that will set like 17 & 102, but in auto layout it was not change.
Constraint :
Constraint Image

You have fixed X and Y Position for your label (Which is not device specific)
If you need to achieve this you need to set X and Y according to Centre of your view
I have create to set centre X (For Y Pos )
And How I have set centre of Y Pos
EXPLAINATION
The centre of UIView according to self.view is 318.5 and required space from top is 20 so final multiplier is 20:318.5
as every view has different centre
For Iphone SE centre would be around 17
Don't Forgot to set First Item to Label.Top as we need 20 from TOP
OUTPUT:
Hope it is clear to you :)

I have attach a image to make a label height dynamically. Select trailing constraint and change relation from "Equal" to "Greater Than Equal". Make sure number of line of label should should be "0".

Related

How to set aspect ratio in stack view?

I am trying to improve my app to looks the same on every iPhone device. I started designing on iPhone 8 plus.
I added 3 buttons to the view and set 3 constraints(width/height and aspect ratio) 1st button = Width:300 Height: 65 aspect ratio
2nd button = Width: 175 Height: 65 aspect ratio
3rd button = Width: 200 Height: 65 aspect ratio
I added them to stackView and set some options(axis: vertical , alignment: center ,distribution: fill proportionally, spacing: 19)
Now i want to set the stackView so it will looks exactly the same on every device.. When i add 3 constraints (top/right/left + aspect ratio) i dont get the results i want. Any idea what i am doing wrong?
Set stackView distribution = FillEqually , then when you give stackView these constraints
top , left , right and aspect ratio
any item will have it's height resized , and give each one the aspect you want so the width will stretch accordingly

iOS constraint equation values

I'm trying to find out what the values of the left and right views in a constraint equation are.
Currently this is how I see it.
The origin-point (0,0) in the coordinate system is at the top left.
Therefore views.attribute that are closer to the top and left are smaller.
In the image posted above.
RedView.Leading has a higher value than BlueView.trailing.
The equation is satisfied because 8 is added to BlueView.trailing.
The same would apply to the circled constraint in the image below.
superView.top is less than greyView.top because superView.top is on origin.x .
My question is are the values relative to the origin point ?
Theory of Relativity in Auto Layout
Short answer:
Yes and no. Actually more no. But most importantly: It's irrelevant!
Detailed answer:
Layout attributes are abstract descriptions of a view's position and size.
Position attributes:
top
bottom
leading
trailing
...
Size attributes:
width
height
While size attributes can describe an absolute value (e.g. view.height = 20.0) position attributes are always relative to another position attribute. That's why Apple only shows two views in their example, without any coordinate system. The equation
RedView.leading = 1.0 × BlueView.trailing + 8.0
states that RedView's leading edge is always 8.0 points to the right of BlueView's trailing edge. The origin of the underlying coordinate system doesn't matter!
Let's say we have a coordinate system ∑1 with an origin O1 and let's assume that BlueView's trailing edge is at x = 100 with respect to that origin. This would mean:
BlueView.trailing = 100
RedView.leading = 1.0 × 100 + 8.0 = 108
Now we look at a different coordinate system ∑2 with an origin O2 that's shifted by 20 points to the left, so
O2.x = O1.x – 20
O2.y = O1.y
In this coordinate system BlueView's trailing edge is at x = 120. So we get:
BlueView.trailing = 120
RedView.leading = 1.0 × 120 + 8.0 = 128
As you can see the values for the layout attributes BlueView.trailing and RedView.leading are different in ∑1 and ∑2. However, the horizontal spacing between the views is the same
RedView.leading – BlueView.trailing = 8
in both coordinate systems.
And that's the whole point of Auto Layout:
To describe the positions and sizes of views relative to each other, rather than using absolute values with respect to a particular coordinate system.
When I tell you to park your car behind your neighbor's car and leave a 1 meter gap in between, you know what to do, right? Without knowing where the road begins!
It's not important.
However – and I guess that's what made you ask the question – the system will need to "tell" the display at some point which pixels to draw for a particular view. And the pixel grid does have an absolute origin and a fixed coordinate system.
So eventually, the system will substitute the layout attributes for the outermost view (the window) before solving all the constraint equations. At that point in time your layout attributes will be relative to a particular origin (most likely the window's origin in the upper left corner, yes) but it's simply irrelevant!
Apple may choose any coordinate system they want (even a coordinate system whose origin is 50 points above the screen) and regardless of that particular system your layout will still look the same with the same set of constraints.
No, values are not relative to origin point. Forget about this.
To position them there must be some additional constraints applied to such attributes of views as:
left, right, top, bottom, leading, trailing, width, height, centerX, centerY, lastBaseline, firstBaseline, leftMargin, rightMargin, topMargin, bottomMargin, leadingMargin, trailingMargin, centerXWithinMargins, centerYWithinMargins.
Also in iOS 9 there were added diffrent kind of anchorPoints to make adding constraints easier.
Also Autolayout added localized leading and trailing attributes which position (leading is at left or right side of view) depends on Device Locale.
I would suggest the following equations:
redView.width = 0 + 1 * blueView.width
redView.height = 0 + 1 * blueView.height
redView.leading = 20 + superView.leading
blueView.trailing = -20 + superView.trailing
redView.bottom - blueView.bottom
redView.bottom = superview.bottom - 20
So it does not matter where origin is.
Everything you are asking requires knowledge of Auto Layout.
Leading, Trailing, Top, Bottom and other several constraints are applied w.r.t to the views.
Example:
RedView.leading = 1.0 x BlueView.trailing + 8.0
here, the leading constraint of RedView is applied w.r.t the BlueConstraint trailing whatever it is. i.e. RedView is placed 8 points farther than BlueView in horizontal direction.
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/index.html
provides a good knowledge of the auto layout constraints, in what context they are applied and the how the views layout according to them.
Also there are top layout guide, bottom layout guide, margins with respect to which you apply constraints to a view.
Read more about auto layout to get a clear understanding.
Edit:
Example:
BlueView frame: (x: 0, y: 0, width: 4, height: 2)
Now the BlueView trailing that we have is: 4
So now we are setting RedView leading as:
RedView.leading = 1.0 x BlueView.trailing + 8.0
i.e. RedView.leading = 1.0 x 4 + 8.0 = 12.0
So now the frame of RedView is: (x: 12, y: 0, width: 4, height: 2)
Also from above equation,
BlueView.trailing = RedView.leading - 8.0
i.e., BlueView.trailing = 12.0 - 8.0 = 4.0
So, the equation is valid for both RedView and BlueView.

Xcode: I've set my UIImageView to both Min and Max width, then why is it always the smallest size?

I've put the following constraints to my UIImageView (Name = RunwayGallery):
Width: >= 200
Width: <= 600
Align Center X to: Superview
100:133 Ratio to: RunwayGallery
Top Space to: ImageAbove, Equals 10
So why is the UIImageView always 200 width? Even when there is enough screen space on both sides (and below) to enlarge the UIImageView to fill the screen.
Many thanks.
Don't define the width of the image view in constraints. Define the padding that you want to the left and the right of the image view. Then auto layout can make your image view as large as possible without growing too large.
You need to add more constraints to tell the UIImageView which width between 200 and 600 to choose.
What do you expect the width of the superview to be? Assuming the superview's width will be between 200 and 600, you can tell the UIImageView to fill the width of the superview by removing both width constraints and adding the following constraints:
Leading Space to: Superview Equals: 0
Trailing Space to: Superview Equals: 0
Now the UIImageView knows to stretch so that there is no space between its leading (left) and trailing (right) sides and the superview's leading and trailing sides.
Alright, I've mixed some of the answers of you guys and came up with this:
Leading Space to: Superview Equals: 0 (priority 900)
Trailing Space to: Superview Equals: 0 (priority 900)
width <= 600 (priority 1000)
This fully works. It makes sense if you think about it. It is filling to both sides with the leading and trailing space, UNLESS it becomes greater than 600.
Thanks for the answers! Appreciated.
EDIT: This is still the same:
Align Center X to: Superview
100:133 Ratio to: RunwayGallery
Top Space to: ImageAbove, Equals 10

Explain Aspect Ratio and respective terms in iOS?

I want to understand Aspect Ratio.
Here, I am setting Aspect Ratio of an UIImageView.
These are options when I click this constraint.
How this constraint works and what are "PRESETS", Reverse Multiplier and Convert to Decimal.
Thanks.
Aspect ratio constraint is used to control the width and height of a view as per a aspect ratio that you set here. There are some standard presets such as 1:1 which means width will be equal to height. Similarly other presets calculates the dimensions based on a ratio
Reverse Multiplier is just used to reverse the ratio. E.g. 4:3 will be 3:4
Convert to decimal just represents the ratio as a decimal. E.g. 4:3 will be 1.33
If you want a view to always maintain an aspect ratio then you can use this constraint. In your case if its image view and you know the aspect ratio of the image that will be set then you can set that aspect ratio as the constraint so that the image is always sized according to the image that is set to that image view,
If you select Aspect Ratio for a single item, the width of the item is
used as the numerator for the ratio, and the height is used for the
denominator. If you select Aspect Ratio for multiple items, Auto
Layout chooses the width of one of the items for the numerator and the
height of another item for the denominator. To change the initial
aspect ratio, edit the Multiplier field of the Attributes inspector
for the constraint. To change which item to use for the width or the
height, use the First Item and Second Item pop-up menus in the
Attributes inspector.
Read more here
Constraints are something like equations in maths.
Ex:
let
X- known value (20)
Y- Unknown value (?)
m- multiplier (like 2 or 3 times)
C- constant (+3 or -3)
to find Y value we use this equation.
Y = m * X + C
Y = 2 * 20 + 3
Y = 43
Constraint equation:
First Object = (Multipler * Second Object ) + constant
width = (0.5 * Height) + 20
In Aspect Ratio condition
Note : one value should be fixed ( Height or width )
A) PRESETS
1)Width = 1 * Height
Width/ Height = 1/1 (1:1)
2)Width = 3/4 * height
Width / Height = 3 / 4 (3:4)
B) REVERSE MULTIPLIER
Before Reverse
Width = 1/2 * Height (1:2)
After Reverse
Width = 2/1 * Height (2:1)
C) CONVERT TO DECIMAL
Before conversion
Width = 1/2 * Height
After Conversion
Width = 0.5 * Height (0.5)

change Auto Layout dynamically

I have to show 3 Labels like
LABEL1 | LABEL2 | Label3 //Horizontally
I want these 3labels width are equally divided according to Screen size width
How can I achieve this with nib file directly?
Thanks
Question 2
Label1 | Label2 | Lable3
//All Labels widths are equally
//All Labels are Subview of a view call X view
Label1 : height is 100
Label2 : height is 150
Label3 : height is 300
Q: Now I want X view's height as max(100,150,300) = 300
Just give top, left , right, height and equal width constraints to all label....

Resources