Custom UITableViewCell that looks like the standard cell - ios

I'm trying to create a custom UITableViewCell with the goal that it should look and resize exactly the same way as the standard cell. The only difference is that my custom cell is using an editable UITextField instead of a UILabel.
It is working fine except for the case when dynamic type is set to smaller than default.
Here's the setup:
Here's how it looks in three different dynamic text sizes:
The one on the very right is the problem. Here my cell on the top is much smaller than the default cell with the "Login" label below it.
How can I make my cell have the exact same size as the default cell in all dynamic text sizes?

Wrap the textField in a simple view, let's call it wrapperView that has top, bottom, leading and trailing constraint to the contentView set to 0; then on your wrapperView set an height constraint that is greaterThanOrEqualTo a minimum value, i.e 44pt (which should be the default cell height)

Related

Build off of default UITableViewCell Swift

I'm trying to build a custom UITableViewCell where the custom title label is correctly aligned with a default UITableViewCell's title label. However, the labels are not correctly aligned as shown below. The custom cell was made in a xib file.
I believe you could increase the left margin of your label (Username) so that it will be aligned with others' labels.

How do I change cell height according to its content?

I have a Detail View Controller, which has different text lengths according to which cell you press in the Main View Controller, of which it segues to. How do I set the cell's height according to how much content is in it?
FYI, the cell consists of 3 labels and 1 image view. It is vertically set as UILabel, UIImageView, UILabel, UILabel.
I am using Swift.
This is what happens when I add the auto-layout constraints:
The way to do it is to use auto-layout. You need to set up vertical constraints from the top to the bottom of your cell:
Distance between the top edge of the cell and the top edge of the first label.
Distance from the bottom edge of the first label to the top edge of the image view.
Distance from the bottom edge of the image view to the top edge of the second label.
and so on ...
Auto-layout will take care of sizing the cell depending on the size of the content you put in your labels and the image view.
You should also remember to set the estimatedRowHeight property on your table view to some meaningful value. This will help the table view defer some of the calculations of the content size to when the user starts to scroll.
Also, set the rowHeight property on your table view to UITableViewAutomaticDimension.
There are 4 things you'll need to do:
Set your tableView:heightForRowAtIndexPath method to return UITableViewAutomaticDimension.
Set your tableView:estimatedHeightForRowAtIndexPath to return the average height of the cells. You can set it to something static like 100.
Set your Auto Layout constraints so that all subviews are tied to each other vertically, and the top-most and bottom-most views are tied up to the cell's contentView.
Set the label's preferredMaxLayoutWidth property either in code or in your Storyboard to be equal to the label's width.
To fix this, I added the sizeToFit() property to my label.

Stretch UILabel to full width of UICollectionViewCell

This sounds like a trivial thing to do, but for some reason I can't figure out how to get this work with AutoLayout.
I have a UICollectionViewCell with two subviews in Interface Builder
UICollectionViewCell
- UILabel
- UILabel
The UICollectionViewCell has a dynamic width height, and so I'd like to set the first UILabel's frame to be something like this using AutoLayout.
CGRectMake(5, 5, collectionViewCell width, collectionViewCell height-10);
However if I select UICollectionViewCell and UILabel, and then click the left-most icon to "Add new alignment constraints", then none of the options are available (e.g. Trailing Edges).
Those menu items were not enabled, because the two items you selected did not have a common superview.
Select UILabel without also selecting UICollectionViewCell.
Auto Layout already understands that the cell is the label's superview, and knows to pin the label's edges to the cell.
Update:
The 'Pin' tool is used for size, or spacing between items. In your
case, you want to set the space between your label and the cell.
The 'Align' tool is used for centering items, or lining items up by
an edge. For example, if you had two labels in your cell, you could
ensure that one label lined up with the other label's baseline.

Working with AutoLayout using UITableViewCell

I am working with autoLayout, I want to place a UIlabel on UITableViewCell, which should be always at the right of the cell and the center of right.
So here is what I want to achieve
So here you can see label named RC which I am talking about.
So here my cell height changes dynamically, so whatever be the text height I always want to be the center right.
2) Now as the cell height depends on the text of the label sentence. I want to make sure that if the cell has the default height and it will not decrease below it. It can increase to any height.
Select your label go to editor then select vertical center in container, also fix it using the trailing space from super view and set fit size to content and it should work. For dynamic tableViewCell height created using autolayout find the TUTORIAL. This will help you a lot.

Have UITableViewCell resize itself with autolayout

I have 3 labels in a UITableViewCell and have the labels set so they will wordwrap. If they word wrap the text goes into the next cell. How do I get AutoLayout to expand the cell based on the content without having to write code in the heightForRowAtIndex method? Isn't there a constraint I can use to automatically adjust the cell based on the contentView?
The cell looks fine if the text doesn't wrap in the label. Once it wraps that is when the problem occurs and I would like to have the cell resize to fit the content and have the same spacing between the bottom label and the bottom as there is between the top and top label.
Unfortunately no, you can't do this. A table view calculates its own total height first and has a fixed idea of the size of each cell as they load, it won't determine it's height from the outside in and it won't let layout constraints change the height of a cell.
If you think about how tables work, with cell reuse, then you couldn't really size the table from its cells without loading in every cell and adding it to the scrollview, and performing a layout pass on the whole thing. That would probably lead to quite poor performance.
You could experiment with populating a "free" cell (i.e a cell you've just instantiated, not added to a table) and laying it out for each row in your datasource when calculating heightForRow.
As you are loading the individual cells, after you fill the labels, but before you load the instance of the cell, check the label height.
Something like:
cell.frame.size.height
If the height is large enough that you know the label has wrapped to two lines, then increase the height of the cell you are about to load.

Resources