How to set height of imageview when no constraints - ios

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.

Related

How to set same size images in tableviewcell?

I need the images in my table to be the same size, such as 40x40 or 30x40. How can I do this?
Give the height and width constraint to imageView and then make the clip to bounds property of imageView to true.

UIImageView frame same as scaled UIImage inside it (with Aspect Fit)

I'm using UIImageView with Content Mode set to Aspect Fit. UIImageView resizes correctly my image but it doesn't change own frame after resizing even there is no constraints set.
What I need:
I need UIImageView to resize own frame according image inside. I've configured image view at Storyboard.
I need to layout again other objects at view because UIImageView's size is changed.
What I've tried:
I've created:
UIViewController (white background) → UIImageView (gray background) → sample image inside it.
Constraints: leading & trailing constraints set to zero, Y constraint is set to align UIImageView vertically.
As you see after image scaled correctly UIImageView's frame is still has incorrect size (gray background).
I've tried to set UIImageView size manually:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let newSize = AVMakeRect(aspectRatio: backgroundImageView.image!.size, insideRect: backgroundImageView.frame)
backgroundImageView.frame = newSize
}
In that example code I've calculated image size and set frame size to image's size. I don't know if it is better way so I don't need to use AVFoundation's methods.
Problem: other object is place incorrectly because UIImageView's size changed and constraints are invalid now.
That methods doesn't work for me:
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
Even I will call my UIImageView size calculation at viewDidLayoutSubviews() other constraints is still using old UIImageView's size.
Questions
How to change UIImageView's size correctly? Maybe I can do it without code? Maybe I need to play with Hugging/Compression? I've tried something but got no luck.
How to force view to recalculate other constraints so they will use new UIImageView size? For example I can put red square at the top of UIImageView, but after UIImageView has been resized by my method, red square still will be at the old UIImageView's position.

How to resize a fixed width and height UIImageView with auto layout

I have a view named myView which is always half the screen and sit at the top of screen.
Inside this view I added an UIImageView with 120x120 size which sits in center of the myView (horizontally and vertically). Inside the IB, to satisfy the constraints(X & Y) I always need to set a fixed width and height for my image, after I set center horizontally and center vertically.
But with a fixed height and width, the image doesn't resize when changing the screen size. I want my image to resize when running on iPhone5 or iPhone 4s, because myView will resize.
I need something like the image should depend on the myView size.
How to actually achieve this ?
set imageView.clipToBounds = yes, and also set imageView.contentMode = UIViewContentModeScaleAspectFit;
Update
You are talking about imageView not the image, in that case it will not resize due to constant width and height, do one thing, create IBoutLet of constraint and change there values when required, or you can also set Aspect ratio with superView
If you set the image with fixed width and height it will not change respectively.
You can either set it to be relative to myView or you can instead set leading and trailing size from all for edges and delete the center constrains.
Your constraints should look like this:
For Swift 4: Updating answer of #Adnan Aftab
imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFit

Cell with Dynamic Height

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".

UIImageView overflowing its containing UIView after calling sizeToFit

My UIView (width: 352px) has a UIImageView subview (default width in Storyboard: 312px).
I want the UIImageView to adapt itself to the dimensions of the image it contains with the constraint that the width of the image view shouldn't exceed a maximal width size (in my case, 312px).
I set up the 'autosizing' configuration of my UIImageView to have a fixed left, top and right margin size. Nevertheless, when I call sizeToFit on my UIImageView and its image is larger than 352px, the UIImageView gets wider than its containing UIView.
Is there a convenient method to prevent such a behavior without doing the math based on the image dimensions? Am I using sizeToFit the right way?
Just use setClipsToBounds:
[imageView setClipsToBounds:YES];

Resources