Scenario:
I have a custom UITableViewCell, and i have added a UIImageView inside it. I can add width, height, top, bottom, trailing, leading, center vertically/horizontally constraints to it, but i want to add constraint for aspect ratio. Storyboard doesn't let me add constraint for aspect ratio with Content View of UITableViewCell, so to overcome it i added another view with constraints top, bottom, trailing, leading (constant = 0) and then made UIImageView a subview of it and added aspect ratio constraint. After that i mentioned height of the cell using
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 60; // for example
}
This works fine.
Question:
Is this the good approach or there is another way to address same Scenario?
P.S : I would have added screenshots but forum is not letting me
Related
I have custom UITableViewCell like this:
Inside UITableViewCell I have UICollectionView, aligned at top with constraints, below it self-made Stepper (view with two Buttons and Label inside), aligned at right by constraints and with fixed width. From the left of UITableViewCell and to the leading of stepper there is an UIImageView, aligned by constraints and also with Aspect constraint (1:1 in preview, i.e. height = width). Stepper's bottom constrained to UIImageView bottom, and also at the bottom of UIImageView another view with Segmented Control and two Labels (named Material). Bottom of UITableViewCell constrained to Material's bottom.
I want to change UIImageView height by changing UIImageView Aspect constraint programmatically (by selecting item in UICollectionView at top) and so height of UITableViewCell, as well as the other bindings, are also changed. For example I want to set Aspect constraint to 2:3 or 4:5, 5:7.
I created IBOutlet for my Aspect constraint inside UITableViewCell class, and try to change it constant in collectionView shouldSelectItemAt method but nothing changes.
What am I doing wrong? I'm new to iOS development, please help me to figure out this issue.
Thanks.
TableView Description
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 60.0
Cell Description
I have a cell with 2 Labels(Title, Description) inside a ContainerView and 1 ImageView as below. Cell height will vary based on Description Label’s content.
Contraints of all views
There are two cases that I should handle
ContainerView.height greater than (ImageView.height + ImageView.Top + ImageView.Bottom). Here cell's height will be based on ContainerView.height
ContainerView height less than (ImageView.height + ImageView.Top + ImageView.Bottom). Here I expect Cell should consider (ImageView.height + ImageView.Top + ImageView.Bottom) as the height and make ContainerView vertically centre to Cell.
Expected Result in both the cases
Problem
If I set constraints for 1st case then 2nd case is not working and vice versa (I’m aware that by removing ContrainerView.Top, Bottom and making it Vertically Centre to SuperView case 2 result can be achieved)
Is there a way to achieve expected result in both the cases by using same set of IB constraints and UITableViewAutomaticDimension?
Give fixed height and width to the image view . Otherwise let the tableViewCell know the top and bottom of the imageview. So that it can calculate the correct cell height
First make sure that you are using self-sizing cells:
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html
Make Top and Bottom constraints for both image view and the container view to the edges of the cell and make them >=.
Alternatively, you could try Horizontal Stack View and make rigid (highest priorities) constraints to each edge of the cell.
Use these delegates,
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100; // height of default cell
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
Edit
Try this one.
Your view hierarchy should be like this
ImageView Constraints
Add a view and put both labels into it.
Label Container contraints
Label Title constraint
Label Description constraint
Edit Output
I've got constraints for a table view cell as follows (the majority are XCode's suggested constraints).
But when the code is executed the row height of the cell is about half of what it is in the canvas.
Initially I didn't have an aspect ratio constraint and the bottom half of the image was missing when the view controller runs, after adding an aspect ratio constraint instead the image is squashed.
I don't understand why this is happening. Why when the height of the image view constraint has priority 1000 is it being ignored?
Try to set the height of the cell in
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
like that it's look like you are not set the heightForRowAtIndexPath so that it tooks the default hight of 44
I have a storyboard with several different types of prototype cells with UILabels containing dynamic data. In my storyboard, the cell looks like this:
The UILabel's Lines property is set to zero to allow multiple lines of text. It is pinned to the top, left, right of the content view and to the nearest neighbor on the bottom (the blue line). The blue line is pinned to the left and right of the content view and to the UILabel at the top, and the UITextView at the bottom.The UITextView is pinned to the content view on the bottom, left and right, and to the blue line at the top.
When I run the app I get the following:
So the UILabel is forcing everything else down, as it should be, but the cell's height does not change as I want it to and thus the text view is being clipped off by the cell's fixed height. It was my assumption that if everything were pinned at the top and bottom, then the content view would be forced to expand. What am I missing here? Thanks!
It seems you have set your constraints correctly, just make sure that delegates are as below:
-(CGFloat)tableView:(UITableView *)tableView
estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
(If your constraints are set correctly from top to bottom)And that's it, you dont have to do anything else, auto layout will do its work smartly.
In addition to Auto-Layout constraint
try cell.contentView.LayoutIfNeeded statement in cellForRowIndexPath just before returning cell and HeightForRowAtIndexPath method
If you're sure all your vertical constraints are set and correct top to bottom, try setting tableView.rowHeight = UITableViewAutomaticDimension explicitly.
I constructing UITableCellView from xib. This xib uses autolayout and two UILabels connected to superview and have vertical constraint.
Unfortunately it is cutting pixel(s) at the bottom of UILabel:
What do I missing?
UPD. Xcode is giving me error: Set vertical compression resistance priority to 749. If I play with compression resistance priority I get first label cut.
Set the priority on the vertical spacing constraint between the two labels to be lower than the vertical spacing constraint between the labels and the top and bottom of the view. Alternatively, in your delegate, override
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
and
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
that will return a height that will accommodate the constraints and label heights. By default I believe you get 44 pixels, so you can get a taller cell by overriding these. Also, you can set the cell height in IB if you're using Storyboards or Nibs.