I am beginner in Swift and Xcode and don't can understand constraints in Prototype cells.
I set that Label ("title top news") get place under Image.
But in tests Label get place exactly in center of Image.
What I do wrong? How doing right and set Label below Image?
Give top, leading & trailing constraints to imageview w.r.t its
superview.
Add height constraint for imageview (if needs to be of
specific height)Shown in attached image -enter image description here
Give leading constraint to label w.r.t its superview
Give top constraint to label w.r.t imageview. Shown in attached image - enter image description here
Constraints for the table cell will looks like - enter image description here
Hope this will be helpful.
Constraints for the imageView
leading ,trailing , top to superView , height (say 200)
Constraints of title
leading , trailing and bottom to superView , top to imageView
you can put the image and the Label in vertical stack
and give them spacing on the attribute inspector.
i hope its help you.
Related
In my UI there is a collection View where each cell contains a UILabel and a UIImageView. There are aligned as follows.
Where I am stuck is setting constraints to them because each UILabel has a random length. I always want to make the distance between left side of the image and left side of the cell equal to the distance between the right side of the label and the right side of the cell (in a certain cell).
I know I can set the centerY of each element equals to centerY of each element. But how can I set the other anchor.
I am not Using Storyboards.
I would add a horizontal stack view, set constraints to every edge with the desired margin, and add the image and label to the stack view.
Then set the hugging priority to the image view higher than the label. This will cause the label to take up the remaining space while being aligned to the left.
If I understand your problem correctly, you want to set spacing for left side of UImageView and right side of UILabel equally. For this, I would recommend you to put those views (UIImageView and UILabel) in a UIStackView with properties: axis as "Horizontal" and Alignment as "Center"; and adding constraint for Leading and Trailing Space with CenterX and CenterY Constraint.
For ease of understanding take a look at the image of UIStackView Constraints
I've been trying to figure this out for days. I can't get the constraints to work on this UITableViewCell. These are the constraints I have.
but it comes out like this
The red is the cell background, cyan is the UIImageView background, green is the title Label, and blue is the percentage Label. How do I fix this? I want to make the row height the same as the image height in the UIImageView but the constraints are making the cell display it's subviews in a way I don't want. I want each cell layout to look exactly like the pictures of the cell I posted.
Firstly, try to always set constraints to closest view.
You have set the bottom constraint of title to the progressbar, for this reason you can see the green view behind the blue. Add the bottom constraint of title to percentage top.
Then, add the constraints of the image to top, bottom and leading, then add the aspect ratio constraint.
In your code add an height to the cell like how you do think it's better.
Make constraint like this
1- ImageView --> top , leading , bottom to contentView & trailing to title & width constraint
2- Title --> top to contentView , bottom to Percentage
3- Percentage --> leading , top to title , bottom to slider
4- Slider --> leading to Percentage & trailing ,bottom to contentView
5- Set vertical content compression resistance for Percentge to 1000
6- Dont't implement heightForRowAt and use automatic tableView cells settings
You're going to want to highlight both the image and the label that you want to equal the same height, click on the "Add New Constraints" button in the bottom right, and then click "Equal Heights".
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
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,
I'm new to iOS and trying TableView for the first time.
I added a prototype cell into the TableView. It has label a inside it which is on the right side. When i run it on lower display like 4S, the label is not visible.
I tried setting Horizontal Space - Content View to label (on right side), but has no effect.
I been playing with the auto layout for sometime, but can't figure it out, can someone be kind enough to help me?
Edit:
Before answering straight, I would say stick to autolayouts and not look for frames, springs and structs.
Now pin UILabel. Select UILabel Go to Editor>pin leading and top space to superview. Also pin the width and height (fix them if you want to). Your problem is solved.
Go through this book for autolayouts and keep playing.- iOS Auto Layout Demystified, 2nd Edition
Updated after question edit :
For Label1 set :
Pin Leading, Top and Bottom Space to superview.
Pin the height(not the width)
For Label1 set :
Pin Trailing, Top and Bottom Space to superview.
Pin the height(not the width) again.
Now select both the labels and go and pin horizontal spacing between them. And if meanwhile you update frame... do update constraint also.
give vertical constraints to your label, if there is not that much need of autolayout you can disable autolayout.
The constraint is not enough to determine the vertical position of the label.
Two optional ways:
Add the Vertical Center in container constraint
Add a height constraint of the label and a top spacing to containe constraint (the contentView of the UITableViewCell your case)
With each of the above and your already exist constraint, the position of the UILabel can be determined exactly.
You can add leading, trailing, top, and height constraints on your label.