I’m trying to create a very simple app where I have an Image View window that I want to keep at 300×300 pixels. Then I have buttons below that depending on which button you click will change the image inside of the image view to a logo I have stored in the Assets folder. I just grabbed the logos off the internet so they are all differing sizes.
I created the image view, set its size to 300×300 and then using the image attribute selected the image I want shown when the app is loaded. In the storyboard, the image is shown just how I want it inside the image view, however, when I build and run the app, the image blows out of the image view box and returns to its original size.
I have setting the content mode on my image view, view attribute to scale to fill as well as aspect fit and neither work when the app runs.
My methods to change the image in the image view are very simple and are just
@IBAction func changeImage(_ sender: UIButton) {
imageView.image = UIImage(named: "name of my image in the assets folder")
}
What do I need to do to keep the images in my assets folder inside of the image view box and keep that image view box set at a sized of 300×300. If you can’t tell, this is my first time using xCode and swift. I feel like there has to be a simple solution I can’t figure out.
2
Answers
I just ended up using a free resizing tool on the internet to resize all of my images to 300x300. I'm sure there's a way to make it work programmatically but I couldn't figure it out.
You can make your imageView 300×300 fixed size from the constraints :
1- choose the imageView
2- add constrains
3- set your width & height
Then you will choose Top, Trailing & leading constraint
It’s preferable to make the image content mode = scaleToFill