Set the position of a label within a cell in tableview - ios

I have created a tableview with dynamic cells. I have added a label in a cell.
The label is always placed in centre of the cell.
I don't want it to be dynamically placed when I adjust the height of the row.
I would like to place the label 20px under the top of the cell.
How can I do this?

You can set constraints like top,leading,trailing and fixed height to that label if you are using autolayout.
Put your label in cell with 20 pixel of distance from top and then set constraints or pin constraints as mentioned above.
Hope this will help :)

Use Auto Layouts in your StoryBoard. Like these
You have to setup all the constraints in the storyboard to make it work properly.

If you use autolayout, set constraint.
Select label and Drag to upper of label by pushing control.
When popup appears, push option key.
Click "Top Space to Container"
enter image description here
Select label and look into size inspector. Modify constant value of "Top Space To" constraint to 20.
enter image description here

If using Auto Layouts, give these 4 constraints, it will work.
Drop the label on custom cell where exactly you want, give top space, tailing and leading and also a fixed height.

Related

Design Self-Sizing UITableViewCell using .xib, cell doesn't adjust itself in interface builder

I'm trying to use .xib file to design a self-sizing UITableViewCell. However, the cell doesn't adjust it's height according to it's content subviews. I have to adjust cell manually. If the cell is too large, a subview will be stretched to fill the cell. And if the cell is too small, there will be some constraint error alert by interface builder.
The UI is correct when running, but is broken when designing.
When using a .storyboard file to design a self-sizing cell, it works great. The cell can grow or shrink automatically.
Is this a limitation of the .xib file? Or did I miss something?
If you set constraints properly, the UILabel will be set its height and width as per the set constrains and its content.
All you need to do is...
For the first label you need to set Leading, top and trailing constraint
For the second label you need to set Leading, top constraint with the bottom of first constraint, and finally trailing and bottom constraint to the superview.
Do not forgot to set the number of line equal to zero & Line break mode to Word Wrap in the Attribute Inspector.
Also need to set the following
We could consider it a "limitation" ... but it's more a matter of "by design."
When laying out UI elements in Storyboard (including a Prototype Cell), Interface Builder (IB) does a lot of additional work. For an obvious example, you can change the Orientation and your prototype cell will automatically expand its width.
When developing a XIB, though, IB doesn't change your root view's size on its own. Think of it as IB saying "you're designing a XIB, you know what you're doing and you know exactly what you want."
While it's generally a good idea to follow IB's advice and get rid of any "constraint error alerts" it's not required.
There are legitimate instances when designing a cell (or other view) in a XIB where you cannot eliminate all constraint warnings. For example, if I put a UIScrollView in my cell XIB, but the scroll view will not have any content until run-time, IB will tell me it has "Ambiguous scrollable content size." In this case, I know my constraints are correct, and I know the content size won't be defined until I add and constrain subviews at run-time... so I can ignore the warning.
If you find it a hassle to manually resize the cell, but not see constraint warnings, you can (for this example) if your bottom-most constraint is Zero, give it a Priority of 999, or set it to >= 0 ... or, change the Ambiguity option to either:
or:
First set top, left, right constraints of first label and bottom, top, right, left constraints of second label with cell.
Set Lines of labels to 0 then it will work as you are expecting.

Constraint does not work in the cell of my table view

When I add some simple constraint for the cell of my table view, all of labels goes to top left of the cell.
Image of cell when it is In story board
but when I run the application all labels and everything goes to left!!!
Image of application when it is running
Try using this button and declare the constraints to a horizontal and vertical positions instead of the pin methods and declare your height and width constraints with it.
To position the labels as you want, you can specify top and left constraints using the "Add new constraints" button in the storyboard:
You should also specify height and width constraints for the labels:

Label cut off on Table View Cell

The top and bottom text label for my tableView cell appears to be cutting off. It is not clipped in the Xcode preview or the storyboard view. How do I make the labels extend to fill the entire row?
I am using a stack view that contains the file name and file size labels.
There are a couple of "potential solutions":
set your constraints (especially the height constraint of the label) correctly
set your cell height correctly. (You probably want to use automatic height)
The layout would be broken if you didn't match these 2 conditions.
This thread will be helpful: https://stackoverflow.com/a/18746930/938380
Maybe you just forgot to set tableView.estimatedRowHeight = ...
Increase the height of label then it will look what you are expected size of the Label.

How to set a UILabel as a percentage of the width of the UICell with autolayout

I'm wondering how to set a UILabel's width expressed as a % of the Cell's total width? I'm using AutoLayout. Normally I would click on the label and set the EqualsWidth to a parentView and then adjust the multiplier so I set the width as a % of the parentWidth. But using InterfaceBuilder that option does not seem to be available within the Cell.
However I'm wondering maybe it has something to do with setting the preferred Width: I'm using ios8+ and have read that implicit widths are not available for ios8?
What I am hoping to achieve is that Label 1 and Label 3 are both 15% the width of the Cell and Label 2 has leading and trailing edges pinned to UIImageView and Label 3 that will allow it to grow and shrink depending on the width of the device.
Steps you should do to achieve your goal of setting percentage width of any subview inside cell:
Add UIView to your cell and pin it to every edge - we have to add custom container view in order to be able to add "Widths equally" pin later on.
Add UILabel to previously added UIView container. Lets say we want to add left side label, so add 3 pins to it: left, top, bottom.
You can see now that you have error in your constraint setup. So lets fix it and add "Widths equally" pin. Select 2 views in View Controller Scene menu - the container and the label. Go to XcodeMenu/Editor/Pin/ and select "Widths equally". Now you should see no errors in you constraints setup and the label should have same size as the container. (Please note that sometimes you have to refresh frames of your views after setting constraints, so if you see "yellow" warning icon don't be afraid and just click on it).
Now if you have all your constraints we need to set actual percentage value. Open tab Size Inspector, click "Edit" title on constraint added in step 3. and change Multiplier value to 0.15 (15%).
Please note that, if you would add label using same technique but for right side, then you would need to set Multiplier value to 6.66 (= 1.00 / 0.15).
Cheers!
You can set width constraint and then create an IBOutlet for that in UITableView cell and change constraint.constant value in layoutSubView method of tableViewCell

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

Resources