Cell with Dynamic Height - ios

I have a UIImageView in custom cell ,Maximum size of UIImagView Can be 100X100. if there is no Image in UIImageView than its height should be Zero. How to specify constraint for this in xib?
Observer left cell image. "There is blank space of height 100 because cell does not have any image to display." There should not be any blank space between hello and heart button.
Right cell image is fine.There is image to display so cell is displaying it.and other component are coming below it.

Put the image inside a div element and set the max-height css property to 100px.

If an Iboutet is connected to the height constraint of the UIImageView then in the custom cell class you may check if there is image set the height constraint a value otherwise set to 0 and the heart and comment components should be pinned to image view.
So that on setting height constraint to 0, rest of the components move closer to "hello".

Related

Capturing UIImageView bounds/frame size with TableViewCell autolayout

I am using the attached prototype cell layout in a tableview. Each cell is sized using autolayout:
tableView.rowHeight = UITableViewAutomaticDimension
The "tagline" is a string and sets the height of the cell depending on the length of the string. Given a greater length the height of the UIImageView also resizes. The ImageView is set to a variety to images with different sizes and aspect ratios. I would like to resize the image based on the beginning width of the ImageView(that does not change) and the resulting height and the image's aspect ratio.
The code to set the two is very straightforward.
cell.taglineLabel.text = tagline
cell.recipeImageView.image = image
Inside a tableView cellForRowAt function I set the tagline first in the code, then the Image. However, if I attempt to resize the image and I inspect the bounds or frame size, the size is the original size. I am assuming the autosize is performing after the func. Therefore, I cannot determine size.
Is there a way to force the autosize after the tagline is set, so I can see the frame/bounds sizes I have to work with?

Set minimum height of table view cell?

I have a UITableView,In this table i have created as custom cell with image & label.I have given label leading,trailing,top,bottom.I want to set the size of label to increase according to text which is happening.I have used UITableAutomaticDimension & EstimatedRowHeight.Now i want to set minimum height of cell equal to height of image.Now if text is very less in label then height of image is decreased.Now i want to keep height cell minimum as image height & increase it when there is more text.Please help how can i do it?
Looking at the view you shared i would suggest the following constraints.
Top of UIImageView to top of your cell.
Left of UIImageView top left of your cell.
Width and Height constraints of your UIImageView.
Bottom of UIImageView to the bottom of your cell which will be greater than or equal to.
Right of your UIImageView to Left of your UILabel.
Top of your UILabel to top of your cell.
Right of your UILabel to right of your cell.
Bottom of your UILabel to bottom of your cell.
Adding these constraints should solve your problems.

How to set height of imageview when no constraints

In this image, height of images in imageview are long. if i try to give constraint height, then the imageview with no image also gets height which i dont want since i want to collapse the tableviewcell with no image.
Its working fine with no image now but only problem is the height.
Follow below steps:
::Put a constraint of height on ImageView
::Take an outlet of constraint
::If there is no image then make constraint.constant to 0 otherwise dont change the constraint.
Images are represented as instances of UIImage class, but this class does not show the image. It works the class UIImageView which has the property image which is UIImage. UIIMageView will present(show) the image contained in it's .image property using dimensions defined in image.size property. So, if you want to change the height, you can do this using the following code:
myImageView.frame.size.height = 50 // or calculate and put here your height
Every instance of UITableViewCell already has an imageView property which is UIImageView and you can access it and control it programmatically.

center custom UITableViewCell

I have a custom UITableViewCell subclass with width of 300.
Since the cell is 20pt shorter than the table view, When the cells get loaded, its placed on the left most position, is there a way to center this custom cell to the tableview's center?
I tried cell.center = self.tableView.center in cellForRowAtIndexPath, but it wont work. any idea?
You can pad your custom cell with 10px to the left so it will only appear to be centered, so you just make the width 310 and move everything else inside by 10 px.
a) Resize your UITableView to 300 and center it.
or
b) Make your cell's width same as UITableView's width. Make your custom cell's background color clearColor. Add one more view to your custom cell subclass that will contain every other view and add every view in that view. Think of this like an illusion where the cell is actually wider but it's contents are centered in a fake view.

How to prevent UITableViewCell backgroundView from stretching

I'm attempting to add a background image to a UITableViewCell so that each cell can have some spacing at the bottom.
I'm setting the cell's backgroundView property like this:
[cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell"]]];
The "cell" image is 20px taller than the actual heigh of the UITableViewCell, this is on purpose so that my cells can have some spacing at the bottom. Only problem is, the "cell" image will get stretched all the way down the cell's contentView.
Is there a way for me to prevent the cell from stretching the backgroundView all the way down?
Thanks in advance!
Look into the contentMode property of UIView. If the height of your background image is smaller than the height of the cell and you want the image to "stick to the top" try setting
cell.backgroundView.contentMode = UIViewContentModeTop;
Look at the "Content Modes" section on this page for more information. It mentions...
"By default, the contentMode property for most views is set to UIViewContentModeScaleToFill, which causes the view’s contents to be scaled to fit the new frame size."
Two options:
1) Make your image bigger manually, filling it with transparent space in the gaps. (Easier solution).
2) Add another UIView inside your UITableViewCell, make it as big as the image. Add your text label (if any) inside this view.

Resources