0
votes

I have set up a UIImageView using AutoLayout. I have set constraints for top, bottom, leading, and trailing (I would like the width and height to change based on the size of the phone). In my attributes inspector, it also shows a width and height for the image view. For some reason when the view is first loaded, the Top Space to Top Layout constraint is ignored and the image goes off the screen, and the height in IB is used (332). Once a photo has been added the view resizes as expected. I NSLogged the frame height of the image view, and it is changing. I do not understand, I thought the FRAME of the image view was always the same. I am not getting any Auto Layout errors, everything is blue. enter image description here

UIImage *imageToView = [existingImage resizedImageWithBounds:self.selectPhoto.frame.size];//Resize the image for better performance
 self.selectPhoto.contentMode =UIViewContentModeScaleAspectFit;
 NSLog(@"Select Photo Frame Size Height is %f", self.selectPhoto.frame.size.height); //The first time this screen loads, this height is 332.  Every other time (after the user enters a new photo, the height is 310 which is what I want.
self.selectPhoto.image = imageToView;

Recap : Why is my self.selectPhoto (which is the large image view shown in screenshot).frame changing? I double checked the Apple Docs, and it doesn't say anything about frame changing based on image size. It matters because I'm resizing my image based on the frame size.

2
you have to give width and height constraint for image view and place image view in a uiview and give that uiview constraints of leading and tralig and top and bottomTeja Nandamuri

2 Answers

2
votes

If you are using AutoLayout.

Step 1: Select the UIImageView, click on pin in bottom right.

Step 2: Add height and width constraint.

Step 3: Select and drag the constraint to your .h file.

Step 4: Set desired height and width in your .m file using below code.

_heightConstraint.constant = 100;
_widthConstraint.constant = 100;
0
votes

I know that imageViews can be funny with AutoLayout when you don't constrain the width and height. Many images are much larger than the imageView and as a result you can see the imageView grow to a huge size (possibly because it can't calculate its intrinsic content size). I've solved this problem before by constraining the width and height of the imageView but obviously you want yours to be dynamic with the device size.

I would suggest using a separate UIView in the project that is the size you want and embedding your UIImageView inside of that. Set the correct constraints for both and this should solve your problem.