Stretch UILabel to full width of UICollectionViewCell - ios

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.

Related

Don't let UILabel overlap another label in UITableViewCell

I have a custom UITableViewCell that has a titleLabel on the left, and a detail label on the right, similar to the Right Detail cell type.
Current neither UILabel has a set width, which is what I want. However the titleLabel sometimes is very long and overlaps the detail label on the right.
How can I give a margin between the two labels, ONLY if the titleLabel is too long and will overlap?
The thing what you can do, is set minimum space between them by putting relation GraterThenOrEqual for example at 10 pixels.
Or there is a dodgy way to it by playing with priority with constraints. You may set priority of constraint between as 750.
In the interface builder you have to set space between the labels, and then select manually this constraint, then you will see the menu
called Relation, just choose there GraterOrEqual

Resizing UIImageView based on UITableViewCell content height using autolayout constraints

I have a UITableViewCell xib with below view:
As shown it has below components:
UILabel - dateLabel
UILabel - addressLabel
UILabel - criteriaLabel
UIImageView - separatorImageView
Since I want to increase the height of cell based on contents in the labels, I added below constraints to each:
So dateLabel constraints are:
addressLabel constraints are:
criteriaLabel constraints are:
Without adding constraints for separatorImageView, cells appear like this:
ie. cells are resizing based on content which is perfect :)
Now problem is - if I add below constraints to separatorImageView:
Cells start appearing like this:
Please suggest which constraints shall I add to separatorImageView so that it resizes as per the height of cell without affecting proper resizing of cell.
Maybe this is not the best answer but for special cases I do this:
create a UIView named resizingVIEW, pin it to top, bottom, trailing, leading of super view (contentView)
remove separatorImageView Bottom Space Constraint
ctrl+click from separatorImageView to resizingVIEW, select equal height
(NOTE set resizingVIEW background color to clear color/ or hide it)

UITableViewCell dynamic constraints

I am making filling a UITableView with custom cells of dynamic heights. I am using the methodology as described here.
For the purposes of simplicity, say my cell contains 2 UILabels as shown below.
The purple UILabel will remain the same height for all cells in the UITableView. In this case, the purple UILabel is taller than the green UILabel. I would like the constraint between the bottom of the purple UILabel and the bottom of the contentView to be 8.
However, there is a second scenario that is possible as shown below.
In this case, the purple UILabel is still the same size as the first case (even though it looks smaller, but since the overall cell height is larger, relatively, it looks smaller), however the green UILabel is much larger. In this case, I would like the constraint between the bottom of the green UILabel and the bottom of the contentView to be 8.
Now my dilemma is that I can set either one or the other (I am using storyboard). But I cannot think of a way to make both possible at the same time. My guess is that you would need to adjust constraints programmatically during runtime depending on the size of the green UILabel.
Any thoughts?
I am using XCode 6.1.1 in Swift.
Thank you.
If the purple label is always the same height, then you should just set the height constraint of the element, rather than setting its bottom constraint to superview as 8.
For the green element, perhaps you could select both that element and the contentview and set them as having equal heights from the "add new constraint" pane at the bottom of the storyboard window. Then, select that constraint and edit its multiplier value in the attributes inspector to some value less than 1. That would make the element's height always be some percentage of the parent view's height. You could tweak that around until it gets you close to the desired 8px from the view below it.

How to use the new auto sizing feature of UITableViewCell in iOS 8?

I have a custom UITableViewCell subclass, and say there is a UIPickerView inside of it. As far as I know the cell requires constraints on both its top and its bottom so that the height can be calculated. The problem is when I specify only the top and bottom constraints, the height of the cell displayed is like 44. So I added a height constraint for the UIPickerView to be 162, then the console says all these constraints cannot be satisfied simultaneously.
So how can I let the estimated height to be 162 so that the UIPickerView is displayed properly (Without overriding the height function of UITableViewDelegate?)

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.

Resources