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".
Related
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.
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.
I'm having a strange issue with my tableview, debugging the view hierarchy i'm getting this
I've tried a lot of constraints with no result, how should I setup my tableview to avoid that kind of misplacement ?
Edit
I've a custom cell with an image view, actually set just this 5 constraints
Top, bottom, leading and trailing values are 0
You just need to set Your UITableView constraint to Leading,Trailing, Top and Bottom. and also for your UIImageView set same constraint. so your imageview will show fit to cell as per table width. no need to set Align Center X constraint.
I am playing with autoLayout constraints in iOS
My use case is this way,
I will better explain with images
I am using a custom cell for UITableViewCell, through autolayout I have set constraints for text and divider line below and they obey autolayout constraints. Now you can see the yellow vertical strip on the left.
I have not set any constraints for it, so when the cell height is normal, it covers the whole cell, but when cell height increases, it doesnt increase and fill the whole cell. I am not able to make its height flexible w.r.t to cell.
I want to expand the yellow strip to cover the whole cell.
So how I can achieve this.
You can make height of yellow strip flexible by setting 2 constraints.
Set both top space and bottom space to container (cell's content view) to 0.
Ensure there is no constant height constraint for it.
You need to set constraints to the yellow view. By default it would apply its own constraints at runtime.
To the yellow view add TOP , BOTTOM , LEADING , and TRAILING constraint. DO NOT ADD A HEIGHT constraint.
Make sure the TOP and BOTTOM constraints of the yellow view are pinned to the cell's content view.
Let me know if it works.
Cheers
I am trying to add a button through IB in a custom cell at top right corner of the cell. I am using auto layout to position it correctly but in simulator I don't know why the button wont show up.
But if i create it programmatically then it shows up perfectly.
So my question is how can achieve it using IB and where i am doing it wrong.
I have added four constraints on my button leading and trailing space to superview and fix width and height.
Remove leading space constrain and add fix to top space constrain. I mean there will be total 4 constrains Fixed width, height and fixed trailing, top space to super view and it will work. If it is not working means you have not set your tableView constrains. For tableView fix it from top,bottom,left and right space from super view.