How to use Xcode auto layout with these Uiimage View? - ios

I'm trying to use auto layout,to make these UiImages views like in the image below, but its always in a mess in one of these devices screens 3.5", 4", 4.7"
so what I have to do to make it like this ?

If you want equal width and equal height of each image then you can add the constraints as shown in below image and if you want to use whole screen space (i.e. height and width of image will be different) then select all the four pins and select Equal Widths and Equal Heights check box (keep Aspect Ration check box unselected)

I think you are missing some constrains, you can do step by step.
Lets number the imageview for easy reference
imgv1 imgv2 imgv3
imgv4 imgv5 imgv6
imgv7 imgv8 imgv9
imgv10 imgv11 imgv12
Drap and drop imgv1, 2 , 3 to the storyboard and place them adjacent to each other. cmd+right click on the three imageview and set constrains(it should be to the nearest neighbour do not forget to untick the checkbox for "from margin"). Based on the figure in the question constrains should be
top - 40, left - 20, right - 20. I am assuming that all imageview are of equal height and equal width, in that case select the three imageview and select equal width and equal height constrain. Dont update the frames yet. It will show error dont worry we will solve it.
Now for 2nd, 3rd, 4th row.
Drag and drop the imageview to storyboard and place them as you want ie.
imgv4,5,6
imgv7,8,9
imgv10,11,12
then select all these imageview(4-12) and add constrains to it. The constrains should be
top - 20, left - 20, right - 20
and as all the image view are equal in height and width, select all the imageview that is imgv(4-12) and imgv(1,2,3) and add the constrain equal height and width.
There is one more thing missing that is we have to add bottom constrain to the last row ie.(imgv10,11,12) for that select these three imageview and add bottom constrain(remember all constrains are from the nearest neighbor).
Then at last update the frames in the controller. It should look like the image in the question.

Related

Constraints not resizing properly for all screen devices

I am trying to do the constraints for these horizontals button. I want the ratio of size of buttons to be the same, and the icons to be of equal widths and heights of each others.
Any idea of how i can do that so these buttons resize properly according to the screen size? Thanks!
Make groups of UIView containing the icon and text. Lets call this container view
Place all the n container views inside your storyboard as you would like them to appear. Now:
To the left most container view add a leading and bottom constraint to the super view.
Now to the second container view add a leading space of 0 (or anything you want). Control + drag your second button to the first button. Hold down shift and select equal width, equal height and align bottom.
Now apply the same constraints as your second container view to all your n - 1 container view. n being the number of container view you want to add. Now to your last (nth) container view add, one extra constraint, which would be a trailing space to the superView. Now all your container view ought to have an equal width that will be determined depending on the width of the screen!
If you want to have a specific height or aspect ratio to all your container view. Just add the height or aspect ratio constraint to your first container view and all your subsequent views will get updated accordingly.
OR
If you wish for the height to be dependent on the screen size and not maintain a specific aspect ratio, then you will have to give the first container view a equal height to the whole view with a specific multiplier like 0.15.
You will also have to add appropriate constraints to the icon and label present inside each UIView
Edit: A much easier thing for you to do would be add the icon as an image to the UIButton and add the text as you would normally to the UIButton. The UIButton will appear quite similar to the screenshot you posted. And then just apply the constraints I mentioned above.
Set the width and height as ratio of the SuperView. Set if for one button and for the remaining buttons make the height and width equal to the first button for which you defined the height and width in terms of superview height and size. Use this SO Post to see how to set height and width as ratio of the superview.
Hope this helps.
It's Simple because your All buttons are in single Direction so you can use StackView.
Just simple first apply the equal hight and equal width to all your buttons
now select all the buttons and add them in the stackview
it will be in the right side bottom. (with the constraint icon)
now simple Apply add missing constraint. it will done the work by own and gives better result. (but take care here apply it from the all views in View Controller Section)
And now Bingo try this every Size will show same.
This will work same in simulator also.
The solution is very simple.
See the image below (5 buttons)
The first (blue) button is pinned to the left and bottom of the superview
Each of the other 4 buttons (red, black, green, pink) are top aligned to the first (blue) button
Each button is using a horizontal spacing to the previous button (with a constant of 0). So red button has 0 horizontal spacing to blue, black has 0 horizontal spacing to red, etc
The Last (pink) button is also pinned to the right of the superview
Finally all 4 other buttons are set to have same width to the first (blue) button
That's it!
As for you icons, all you need is to set them to have same width & height to the first icon you have

How do I fix layout issues with constraints in Xcode?

I used the following constraints to build one screen for my app. However the icon buttons get messed up in different screen sizes (screenshots below). Is there a way to rectify this?
1- Give equal heights and equal widths to all 4 boxes.
2 - For first one-> leading, top,
- For second one(travel and place)-> horizontal spacing to 1st one,leading and top
- For third one(activity)-> vertical spacing with 1st one ,horizontal to utilities and leading
- For fourth one-> trailing , vertical spacing to 2nd one and horizontal spacing to activity
What I would do is:
Set a proportional size (width and height) to the first button, maybe 40% of the container.
Then for the other 3 buttons, you can set them to the same size as the first.
Add some space between them.
And thats it.
1 - Take two views as two rows
2 - Let for first view (row), add two subviews , give the width of first equal to width of superview and multiplier 0.5 , let the second view align leading to first view trailing and width equal to first view
3 - add the button as subview in each view with top , right , bottom and left spacing as desired. Repeat the same for 2nd button in 2nd view.
4 - Repeat the above steps for row 2 view
hope this helps
These are actually pretty straightforward when you think about how all the views are supposed to relate to each other. I made a demo storyboard to show you how I'd wire it up. The key things:
Each box has a 1:1 aspect ratio.
Boxes in a row have equal widths to each other
Each box is constrained on both sides to the edge of the view or each other
The icon's height is set to a % of the box's height
The labels are pinned to the top, right, and left of the box
Obviously you'll want to adjust your styling as appropriate, but that should get you the effect you want.

How to expand to Labels having height in ratio 2:1?

I'm new to xcode storyboard. I have a viewcontroller that contain two label of same width, but label 1 is 80px in height and label 2 is 40px in height.
I want those two labels to auto expand in height depending on whether they are displayed in horizontal or vertical size class, but I want label 1 and label 2 to keep their height ratio of 2:1.
I know the steps to take if I wan't both labels to be the same height (control drag from one label to another, click equal height, and go to size inspector and set their content hugging priority to 200) , but I don't know the steps to take when I want the labels' height ratio to be 2:1 and to auto expand in height depending on whether it is displayed horizontally or vertically.
I want the two labels to expand, not the numbers below the labels
If you want label 1 to be twice the height of label 2, then control-drag from label 1 to label 2 and select Equal Heights from the pop-up menu. Then find this constraint in the Document Outline and edit it in the Attributes Inspector on the right. Change the multiplier to 2.
If you pin the orange label to the top of the screen and pin the bottom of label 1 to the top of label 2 and pin the bottom of label 2 to the views below, then these two views will fill in the space and maintain their relative vertical sizes.
To set the aspect ratio, Ctrl+drag from one point to another on the same view. Enter the aspect ratio in the multiplier.
For auto sizing, set the number of lines in the label to 0. The label will expand in the direction without a constraint (or the direction with the lowest priority constraints).
E.g. if you want the label to expand vertically, leave off the bottom constraint. To expand horizontally, leave off the right constraint.
You can simply do this :-
Take two labels:-
Click on first Label and add the following constants:-
now your label1 will look like this:-
Click on second label and add the following constants :-
Now both of your label will look like this:-
Update the frames :-
And run your App:-
In Landscape
In Portrait
Note: if you want to set them in there ratio you can do this

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 : Size classes issue

I'm creating a UI using size classes. As you can see in the below picture , there's issue with iPhone 6 Plus . Please help iPhone 4s always creates a mess. Do tell me if the UI is correct?
Image description:
Hope this helps!!
Select Login Merchant button, Blue and Pink button with OR label and put them in Stack View with Vertical axis. name it as (Bottom StackView)
Put some proper spacing between them.
If your image height is not fixed then make it relative to the device height, that means create a height constraint of imageView = (1/3)height of mainView.
Also you can take Label and textField Username in Horizontal StackView then combine both textFiled (username and pass) and Label in Vertical StackView.
Make vertical constraints Greater Than Equal to 20 between Bottom StackView and Center StackView.
this will give to better result in all screens.
In Case if you don't use StackView then,
Combine Bottom 3 button in SingleView (BottomView) and Upper textFields and Labels in SingleView (CenterView).
Then set vertical spacing s Greater Than Equal to 20 between them.
Make your image height as relative to screen size as described above.

Resources