Adjust Image in UIImageView

I have a UIImageView and four buttons below it. Once the user taps one of the buttons it sets the image in the image view. The problem is I want to add constraints to the images in the image view. I added the images programmatically the code for one of the buttons looks like this:


overridefunc viewDidLoad() {
        super.viewDidLoad()
        let defaults = UserDefaults.standard
             
        goldImageView.image = UIImage(named: "gold with Text"
    }


@IBAction func gold(_ sender: Any) {
        goldImageView.image = UIImage(named: "gold with Text")
    }

Accepted Reply

To make OOPer's description even more clear --- you will be setting constraints on the UIImageView not the UIImage. You can do that in the XIB or Storyboard. Then you will be adding a UIImage to that UIImageView. You can add that UIImage programatically. When you switch the UIImage in the UIImageView it will not affect the constraints. If you want different constraints on the different UIImages then you can create multiple UIImageViews each with its own constarints. Then when you tap those buttons you will need to set the "hidden" value for those different UIImageViews.

Replies

>I want to add constraints to the images in the image view.


See this SO thread: https://stackoverflow.com/questions/37907281/xcode-adding-constraints-so-an-imageview


Assuming you're not tied at the hip with only doing this programmatically, because, why would you when IB has built-in constraint control.


Set constraints on the view(s); or, maybe scale the images...it really depends on your goal(s).

What constraints do you want to add exactly ?


Do you need to add those constraints to the image or to the imageView (would make more sense to me), depending on the image itself.


Why don't you

- create a priori constraints in IB,

- connect IBOutlets for the constraints you want to change

- when loading the image, adapt the constraints constants or activation depending on the image characteristics ?

The constraints that I want to add are to the top bottom and sides of the image, and constrain the images to the image view. I want to constrain all of the images in the view for example if I tap on the first button I want the image associated with that button to have constraints, and if I tap on the second button and it changes from the image from button 1 to button 2 I want the image associated with button 2 to have constraints also. The problem I am having is that I don't know how to add the constraints to the images. The images are added programmatically they are not in the main.storyboard.

UIImage is not a view and the layout of a UIImage inside UIImageView is not defined by constraints.

You can control how an image is desplayed inside UIImageView by changing the property `contentMode` of the UIImageView.


If you want more control about the layout of the image, you may need to embed the UIImageView in another UIView, and add constraints between the views.

So you want images to have always the same size ? Please explain exactly what you want to get, with figures not just statements as "the same constraints", as we don't know what they are.


But as OOPer described, you will have to consider if you want:

- to keep the full image

- to scale to keep proportions…

So you may not be able to set constraints for both width and height positions and size. Depends on what you want.

To make OOPer's description even more clear --- you will be setting constraints on the UIImageView not the UIImage. You can do that in the XIB or Storyboard. Then you will be adding a UIImage to that UIImageView. You can add that UIImage programatically. When you switch the UIImage in the UIImageView it will not affect the constraints. If you want different constraints on the different UIImages then you can create multiple UIImageViews each with its own constarints. Then when you tap those buttons you will need to set the "hidden" value for those different UIImageViews.