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.
Related
I have created a UIView that consists of 3 UILabels and 1 UIImageView as below -
The UILabels have lines set to 0 so that the height of the UILabels change dynamically
I have added constraints such that the height of the image depends on the height of the UILabels as shown below -
I have set width of the UIImageView to a constant of 100.
I would like to set a constraint on UIImageView such that the height of the UIImageView is equal to the height of the whole view.
I tried adding a "Equal Heights" constraint on the UIImageView and the whole view. I observed that the height of the whole view depends on the height of the UIImageView and not that the height of the UIImageView depends on the whole view as I want it to be.
Can anyone point out how I can add a constraint to the UIImageView such that its height is equal to its superview but the height of the superview is not dependent on the UIImageView?
Edit -
The below image shows the problem that I am facing. When an image is set to the UIImageView, the height of the view changes.
I want the height of the UIImageView to be equal to the height of the view, but I do not want the height of the view to be dependent on height of the UIImageView
Looks like you need to reduce the vertical content compression resistance on the image view.
Set it to something very low like 100 and try again.
I believe you haven't add the height constraint to the superview. Setup height constraint for the superview and then add a "Equal Heights" constraint on the UIImageView and the whole view.
In fact default compression resistance of your UIImageView is 750, while content hugging of root UIView is 250. This means AutoLayout engine layouts everything correctly. You can change vertical compression resistance to value lower then 250 (UILayoutPriority.defaultLow - 1) to achieve desired layout.
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.
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.
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
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];