Autolayout TableView Issue - ios

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.

Related

Xcode missing constraint warning for y position but it's set

I have in the same view controller two UITableView. Both UITableView cells have an UIImageView, a top UILabel with top and lead constraints, a bottom UILabel with lead and bottom constraint. Cells are set the same for constraints but on the second UITableView cell Xcode gives me a missing constraint warning for y position on bottom UILabel , when it actually has a bottom constraint. If I add a top constraint for the bottom label in the second UITableView cell the warning goes away, but it's not needed for the first UITableView. What am I doing wrong?
As always many thanks.
At Storyboard at this case maybe will be better embed your labels at Stack View? And this will make your Cell more changeable and you should better to maintain it?

How to set UITableView cells width to same as its parent/device width?

I have a UITableView cells that won't follow it's parent width. I found so many codes for how to change the height, however I couldn't found codes/ways to change UITableView cells to same as it parents.
Some of answer told that it would follow automatically. But in mine, the problem is the width can't follow device's width. Is there anyone that know how to change it?
The UITableviewCell width will always be the same width of the UITableView.
If you want to set the width of the cell same as the parent of UITableView, You should set the width of the UITableView same as that of parent
If you want to set the width of UITableView the same as it's parent view, set the trailing and leading constraints of your UITableView to it's superview with value 0
you need to remove the width constraint and aspect ratio that you have added to it. Instead of ctrl and drag, select the tableview and click the pin button and set the constraints as shown in image. Remember the trailing and leading values must be 0 for setting the width as same as parent 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.

Applying autoLayout constraints to multiple objects

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

Adding UIButton to custom cell with auto layout

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.

Resources