Resizing UIImageView based on UITableViewCell content height using autolayout constraints - ios

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)

Related

UIScrollView Autolayout height not setting on dynamic content

I have two labels that are inside a stack view that is encapsulated in a scroll view. The layout structure is like the following:
The layout looks like so in the interface builder. (This is the view with the contentView selected)
The Content Stack View is pinned to the contentView's via top, leading, trailing with 8pt on each.
The labels are set at runtime and can be quite tall. This cuts off the text and causes the scroll view to not be scrollable. (Cuts off at bottom)
I've went through a few other similar questions on here but was unable to find a solution to mine.
Some are resources I went through I looked at were Mokagio, and Natasha.
I have been on this for over a day now and am completely stumped.
How does the Scroll View's content size become scrollable from the generated content?
Please follow below steps:
Add UIView into UIScrollView and then add both UILabel into UIView.
Set top, bottom, leading, trailing constraints to UIScrollView and UIView
For both UILabel set top, leading, trailing, bottom, width, and height constraints.
lastly edit height constraints to set - priority High(750), select >= from constants dropdown for both UILabel
IMPORTANT: set UILabel's number of lines to 0
Please see below screenshot for UIlabel constraints:

UITableViewCell with two UIStackViews

I'm facing a layout issue with a UITableViewCell which consists (from top to bottom) of (1)a UIStackView, (2)a UIView with a fixed height and (3)another UIStackView. In the top UIStackView I want to place a few instances of UILabel vertically, depending on some datastructure. These labels can vary in height. In the bottom UIStackView I want to place a few instances of UIButton vertically. This number also depends on some data structure.
The problem has to do with AutoLayout (at least I think it does). How would I set up the constraints in such a way, that the UITableViewCell creates both enough space for the top UIStackView and the bottom UIStackView when it needs to be placed on screen. The layout of the cell is defined in a separate XIB file.
So far I have a heightconstraint on the top UIStackView. Depending on the frameheight of the labels the height of the stackview is adjusted. But it turns out, that the height always has the same value. I know this, because I need two instances of this particular UITableViewCell in my TableView.
The bottom UIStackView does NOT have a heightconstraint and has always a correct layout.
Do I need to adjust the properties for vertical hugging/compression resistance priority?
Any help is greatly appreciated.
Maybe I misunderstood your issue, but why are you fixing the height of the top StackView if it's height is supposed to be dynamic ? You should remove this constraint and set intrinsic sizes (below "Content compression resistance priority") of both StackView to "placeholder" to keep IB happy.
The top StackView will then behave the same as the bottom one, provided you don't have other constraints issues.

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.

Autolayout TableView Issue

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.

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