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,
Related
I'm trying to figure out an approach to the following:
I have a square image, I want to align it horizontally and vertically in the centre of the screen and constrain it in a way where its left and right edges are 60pt from screen edges.
I got thus far, but am now getting an error from XCode about top and bottom image constraints. How can I make it so image keeps its square ratio and auto resizes based on screen size?
Example, images width became 294pt in screenshot below, yet height remained unchanged, where as it should change to 294 pt as well.
You need to add 3 more constraints.
Vertically align your view to superview
Horizontally align your view to superview
Set aspect ratio of your view to 1:1 (Important)
On the whole you should have 5 constraints. The screenshot is given below.
this error occurs when auto layout did not find enough information about a component on the storyboard. as you did it here you only provide the trailing and leading constraint so now it asks for height and width.
if you want the same height on every device selects the height constraints that will fix the heigh 300.
Or simply you can do this set the alignment constraint to Center vertically and horizontally and then add top and leading constraints
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:
I have this images views:
I want that for every device that the distance between the first imageview and the left side it's the same , then the distance between the first imageview and the second image view it's the same and the distance between the second imageview and the right border it's the same. Essentially It must grow their width according to the screen size of the device , but I don't be able to do this by autolayout on storyboard.
Can you help me?
Try the auto layout in below mentioned way
Suppose your two image views are imageVw1 and imageVw2
Consider imageVw1 is left one.
So put following constraints for imageVw1
Leading constraint with main container view.
Vertical constraint with main container view.
Fixed height Constraint.
Horizontal constraint to imageVw2.
Similarly constraint for imageVw2
Trailing constraint with main container view.
Vertical constraint with main container view.
Fixed height Constraint.
Horizontal constraint to imageVw2.
IMPORTANT : Provide equal width constraint to both imageView. (this constraint is for assigning equal width for both the image view. Else we need to give variable width, but i guess you need both image would be of equal width. So just assign equal width constraint).
See output for 6sPlus and 5s.
Hope it helps.
Happy coding ...
Make a leading connection between left image and the view, left side. Make a trailing connection between the right image an the view, right side. Make an horizontal spacing connection between the images. Make a connection over one image and select aspect ratio and repeat it in the other image. Make connections to align, y position, etc
First imageView add a leading of lets say 10. And then between the second imageView and first imageView add a spacing of 10. Add a trailing space of 10 units of the second imageView to its superView. Now set second imageView to have an equal width and equal height to first imageView.
What you need here is setting equal widths property for your left and right images. This can be done by selecting two images at the same time and by checking 'Equal widths' flag.
You should also pin the leading space of left image to view with 'x' offset and pin the trailing space of left image to 'x' offset to right image. Also pin the trailing space of right image to 'x' offset to the view's trailing.
If the image view's super view is a scroll view, you will have to set the content size property of the scroll view.
May be you can go through this link.
Adding a view to a scroll view that will stretch to fill available width
Please look at the ScreenShot attached for wCompact|hRegular for different screens, I am trying to make it working since hours but not getting any success. My requirement is that at the top there would be a label with some predefined margin. Although the Label content would render at the runtime, but I know the content size, so resizable label isn't needed actually I think. Now there would be three row at equal distance. In first and third row, there would be two buttons with equal height and width and in second row there would be button aligned horizontally. I have set the buttons image and text in storyboard. Control Alignment are set to Horizontal | vertical. Constraints for label are:
Pinned top space to superview, leading and trailing space equals to:8(superview), height equals:90.
Constraints for Button(View Transactions) are:
top space to label, bottom space equals to:8(New Launches Button) leading and trailing space equals to:8(superview) and 8(Place Request Button) respectively, and equal width and height for all buttons.
Looks fine for 4.7 and 5.5 But not satisfied with the output for smaller screens. As u can see resizing of buttons image not working properly(Larger space between button's image and text). One more thing is I don't wanna set the height of the label, cause it seems like a wrong practice in AutoLayout. Any help would be much appreciated.
You should set the Aspect Ratio for the Buttons, not just the equal width and height. In that case auto layout wont shrink the images.
Really you should put this into a scroll view so that if the height is too great the user can still see everything by scrolling. You should also not set static heights on labels, you should allow the intrinsic content size to apply.
Add a subview to your new scroll view. Pin the width of this subview to the width of the scroll view. Do not pin the height.
Add all of your buttons and labels to this new subview. Pin them to the edges of the view and allow the intrinsic content sizes to apply limits. Set various items to have equal widths and heights. Do not set explicit heights or widths (do everything by proportion or equality so auto layout can choose good sizes).
Hi I need to display 3 or 4 buttons based on some condition for that I am using following constraints (image below).
It works fine when I have 4 view, but for 3 buttons, first button is not sized equally, but rest of the buttons are aligned with equal width (image below). I have set priority 990 for horizontal spacing constraint, so rest works fine.
I am setting all the autolayout constraints in storyboard.
How can I achieve equal width when I remove middle view?
iOS9 only apps:
This might be a good time to use the new Horizontal Stack View. Just set distribution to fill equally and alignment to fill
For iOS8 support:
You'll have to set the width constraint to equal width between the components and the horizontal spacing to 0
Set button1 to equal width with button2 then button1 to equal width with button3. This should be enough.
Create 3 buttons, and set their width and height equal to parent(Superview).
Next set the multiplier property on "width constraint" for each button with "1/3" value like that: