Issue Regarding How to Put Aspect ratio in Ios. - ios

As there is super View in that Super there is another View having Height 100 and width 270. i have given constraints like this for inner view (Image) Given Top =5, Leading and Trailing 25 25 and Aspect ratio 320:100(Width of Super View and Height of Inner View), When i add this constraints then size of inner view changes accordingly Means Works Fine. But when I add TextField and Label inside View then Size of label and Textfield Not changes Accordingly Please Specify me what aspect ratio I give To Label and TextField. Please Thank You

Aspect Ratio is used for like as the width increases the height of that particular object also increases with that ratio vice versa case if height increases but giving an object just aspect ratio doesnt mean that the objects dimension will increase/decrease automatically, you have to give other related constraints also to make your object's dimension change as the view's dimension chnage liek for example giving proportional height or width to that object with respect to the view then as the view increases or decreases the dimension of that object will also change.
Give equal height to the container
Set the multiplier
The same you do with textField and with bottom label you give Leading Training equal Height and vertical space with the top label and In textfield also the same.

Click the second UITextField drag it to first set leading ,trailing
,equal height ,equal width.
Then set horizontal space to second label.
This constraints make it proper .
If you want aspect ration drag with in the UITextField set height
constraints and width constraints (or)Aspect ratio Constraints.

Related

Xcode How to create a square button(width equal to height) inside a Horizontal stackview

I have a requirement to create 3 square buttons at bottom of the page. All of them must be aligned and their size should be square.
So far, I have created a horizontal stackview and created autolayout constrains 50 from the left, 50 from the right, 10 from the bottom and set the stackview height as 60.
How can I create square buttons, since the stackview subview width is dynamic and determined at run time?
How can I set an equal aspect ratio for these three buttons?
Don't set the stack view height; let the content determine that.
You haven't said exactly how you want the buttons laid out, but I assume you want them equally distributed.
Put the buttons in a horizontal stack view
Constrain the leading, trailing and bottom edges of the stack view to the superview
Set the stack view's alignment to "Fill" and distribution to "equal spacing"
On each button, create an aspect ratio constraint between its width and its height of 1:1
If you want a minimum height for the buttons, set a >= height constraint on one of them
If you want a fixed height for the buttons, set a = height constraint on one of them
I think the only thing you are missing is setting the Distribution in Attributes Inspector on your Stack View. Set it to distribute equally as shown in the image below:
To set the height, you simply need to set a height contraint on each button. Click the tie fighter looking icon and set the height for each:

Xcode autolayout: best fit for square

I have a custom square view that can grow and shrink as needed. My custom view just overrides drawRect to render the content.
The containing view is a rectangle, but not a square. The aspect ratio changes depending on device orientation and type. I need to fit my custom view so that it takes maximum space in the container, but retains the 1:1 aspect ratio.
I would like to set the constraints in IB rather than code if possible. I have the following constraints:
Vertical centering in the container
Horizontal centering in the container
Aspect ratio (1:1)
Content mode set to scale to fill
I tried various things to make the custom view fit the container, which give me IB layout errors, for example:
Width <= container width
Height <= container height
I can't set any of the sizes equal, because I don't know if the container is vertical or horizontal...
This seems like a basic task, but I can't figure this out. What is the best way to fit square in a rectangle?
Keep the constraints you already have, and add new constraints:
width and height equal to the superview, but with lower priorities than the aspect ratio constraint
width and height <= the superview size
first three constraint are right., remove others and set the
leading or trailing and
top or bottom
constraint to superview
Now select the newely added constraint and make them >= (greater than or equal) to 0.
now you'll need to give the width or height constraint to your view which it can have it from its subview's same as we do it for scroll view or give a width equal to 320 to you view and make it also greater than or equal to.

Constraints not working

I am not sure why but I have having so much trouble with constrains and auto layout. I have the below view and I want to make it display as is shown but every single approach I take to get seems to be incorrect.
I have been following this tutorial online Auto Layout Tutorial in iOS and I am rather trying to describe my constraints. Following this technique this is what I have:
Description of my constraints.
Label:
Centered in the view
51 from the top
All images:
Must have equal height and equal width.
Are separated from the left and right by a 0 gutter.
All inner gutters separating the images are 12.
I have also included my storyboard.
storyboard link
for this you just declare height and width of the first imageview with respect ViewController use equal heights and equal widths and then change in the multiplier default it will be 1 adjust to your size then for the remaining imageviews you just give equal widths and equal heights of the first imageview.
If you want keep height and width constant , don't pin it, just use horizontal centre constraint. Pin it up using auto layout , hope this is helpful.
You are set image height and set image bottom constraint and does not set label height that you are many choice
Set your label height because you run iPhone 4 or 4s that your label doesn't display.
remove your image bottom constraint because you are set fix image height.
you image doesn't fix height that remove your set image height and all image select and set equal height constraint (all image height same).
Better You should move that four image views into a uiview(childView) .Make sure that 4 image views are the subviews of UIView.
-Add equal width and hight constraint to SuperView from the childView.
- Add multiplier value for the EqualHight and Equal Width constraints by selecting both constraints on storyboard(like 1:2.1,1:2.2).it may keep the same distance in all orientation
Add Y position and X position by trailing space and leading space, centre vertical in container.
Then Select the 4 uiimageviews and make sure they have same width and hight,after that
select 4 image views then click the pin button and check the equal width and equal hight.
Add leading ,top and trailing space for image1
[1,2]
[3,4]
add trailing and top space for image 2
add leading ,top and bottom space for image 3
add trailing ,top and bottom space for image 4,

iOS Autolayout: Is the Size Constrains for UIView is Obligatory?

I am trying to arrange controls in the storyboard using constrains via Interface Builder ("Any Width, Any Height" case). I add there UIView, however, when I press update frames for this element (Selected Views case) according to the given constrains UIView disappears from my view. I guess it size becomes zero. Later on, it is not show up in the screen after the run. The issue is also reported in the issue navigator: Horizontal Position of UIView is ambiguous (marked with the green shape). Anyway, if I set the constrain for width or height (size parameters) together with the constrain of ratio, then the issue disappears, I can update frames, it shows up in the screen during the run.
I am adding the picture with the green marks to make the issue more clear:
The question is whether the parameters of the size are obligatory when I am setting the constrains.
Even if they are obligatory how I can make the size to fit different sizes of the screens or iPhone+iPad because if I set/fix the size it could too big for some screens.
The size is not mandatory. The warning tells you that you must set an X position for this view (constraint between the superView left or right border and the left / right border of the view itself). If you want the view to resize itself to fit each screen size you must set both left and right constraint without the size constraint.
You shouldn't used fixed size (width or height) for view that change size when screen dimension change.
If you want view that fix aspect ratio you can set size that relative to Superview's width or height using:
1) leading and trailing and make sure that
view.leading = superview.leading + fixed_margin
view.trailing = superview.trailing - fixed_margin
and not other way around.
2) equal width or equal height with multiplier, constant relative to superview as you want
So you don't need evil size class.

How to make my label one-third of my UITableViewCell through auto-layout

I have a TableView inside my View Controller and inside my TableView, I have a Custom UTableView Cell. I want to have a label one-third or my custom tableview cell, but when I drag by right clicking on my label to the content view (Of my cell) to select aspect ratio, I don't get the aspect ratio option. Can anyone please help me to figure out ho to make my label to be one-third of my tableview cell.
I set the constrain for trailing space and top space to container.
Now, when I go to find the aspect ratio, I don't get any.
Aspect ratio probably isn't the best for this. You should drag from the UILabel to your content view and select an equal width constraint. Then, open the attributes for it and change the multiplier to .33.
Just to add to #dreaming-in-binary's answer, IB may not allow you to set an equal width constraint against the content view (even though it is possible programmatically). Instead you could add a hidden reference view which spans the full width of the cell (i.e., has leading and trailing constraints to the content view with a constant of 0, and any vertical constraints). Then you can set your equal width constraint (really a proportional width constraint) against the reference view instead.

Resources