Swift Table Cell Labels in wrong order - uitableview

I have a table cell set up with two labels. Title and subtitle. The labels are configured using autolayout. Title is pinned with top, left, and right at 20pts (with not constraints). Subtitle is pinned with left, right, and bottom at 20pts (with no constraints).
But when I run the code the subtitle is above the title. If I try to pin the top of the title label to the bottom of the subtitle label with autolayout I get the error "Unable to simultaneously satisfy constraints."
How do I place these labels in the proper order?
Thank you!

Related

Constraints in UITableViewCell not working

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".

iOS How to fix UIButton height in a custom UITableView cell

I have a custom table view cell with two buttons. The accompanying text has a varying number of lines which affects the height of the cell. This height variations causes the height of the buttons vary. I have tried a lot different things to fix this with no luck. Currently the two buttons are in a stack view and that is in another stack view with the UILabel. I'd like the buttons with a fixed height vertically centered in the cell. What's the best way to do that?
Use only one stack view - for the buttons.
Constrain your UIStackView to the trailing edge of the cell's content view, and constrain it centered Vertically.
Constrain your UILabel to the leading edge of the cell's content view, and constrain it centered Vertically.
Also constrain your UILabel horizontal-spacing to the UIStackView, and edit it to be >= 8.
And, the key part, also constrain your UILabel Top and Bottom to >= 4.
Now, when your label wraps to multiple rows, it will "push the top and bottom", and your stack view holding your buttons will remain vertically centered, but will not "grow in height".

Autolayout equal spacing and weighted spacing

I'm trying to use autolayout to create a layout for cells in my table view. I will attach a screenshot of what I am trying to achieve.
The main rules are that the button at the bottom must be the width of the cell. Then the image on the left should sit tight to the left but exactly halfway between the top of the entire view and the top of the bottom button. Then I need the 4 rows on the left to sit equally between top of the view and top of the button. And for them to display in a list like they do, and with a small image and text label in line.
I have tried quite a few ways to get this to work. I have managed to get the button to stay at the bottom as required. However, I am struggling to get the imageview to stay as a square on the left and then the 4 rows on the right to take up the rest of the space.
Any help would be appreciated.
You can achieve what you want by wrapping imageView and rows into separate container views (see
Attachment)
Steps for imageView:
ContainerView1 constrained to the top of the cell view and to the top of Check In button
imageView constrained vertically to the center of the ContainerView1. Also set imageView leading constraint to the ContainerView1 leading as you need
Steps for rows
Wrap rows into ContainerView2. ContainerView2 constrained to the top of the cell view and to the top of Check In button.
Constrain firstRow top to the top of ContainerView2. Each subsequent row should be constrained by its top the previous row's bottom. Constrain lastRow bottom to the ContainerView2 bottom
For all rows except firstRow drag "equal height" constraint to be equal to the firstRow height
That is all!
P.S. if your application works starting with iOS 9.0 you can use UIStackView instead of manual setting constraints in your cell
To keep the images square you need a 1:1 aspect constraint.
Hard to know what else is going wrong with your layout without seeing your constraints but for reference this is how I'd lay out this view.

iOS UITableView cell fit to screen width

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.

Dynamically set the height of UITableViewCell to wrap content

I'm trying to display 3 image views, along with some additional information in a UILabel, in a UITableView. Attached is an image of my Cell Prototype:
Currently, my issue is that the image content is running over to the following cell. As I test, I'm currently only loading the largest image of the 3 (I pull them from a server presorted from largest to smallest-- I'll also note that the images will be subject to various dimension constraints).
My issue is that, while its width is working correctly, its height runs over the metrics label and into the next cell.
I now know that in order for this to work, auto layout constraints have to be properly configured. However, I'm new to this and am having some trouble with it.
To start, I'll try and explain exactly what I'm looking to accomplish:
The description label should span across the entire table cell.
To do this, I've pinned the leading, top & trailing spaces to the superview
I would like the top right and top left image to both be positioned directly below the description label. To do this, I've:
Pinned the leading space of the top right image to the superview
Pinned the trailing space of the top left image to the superview
Pinned the top space of both images to the Description label
Pinned the horizontal spacing between these two images
I would like the bottom right directly under the top right, to do this, I've:
Pinned the leading space of the bottom right image to the superview
Pinned the top space of the bottom right image to the top right image
I'd like the metrics label to appear below the top left image, to do this, I've:
Pinned the top space of this label to the top left image
Pinned the bottom and and trailing edges of metrics to the super view
Pinned the leading space of metrics to the bottom right image.
I'm certain I'm doing something wrong here, should I not be pinning subviews to each other at any point?
Also, apologies for my presentation of this issue. Below is an image of the warnings I'm getting (for your reference, Image 1 is top right, Image 2 is top left, and Image 3 is bottom right) When I view the constraints of each of the image views, each side IS accounted for.
Any help would be appreciated! Thanks.
try adding this code to your viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = tableView.rowHeight
tableView.rowHeight = UITableViewAutomaticDimension
}

Resources