cropped image when updating interface

21 Views Asked by At

I have set constraints for the image, the stock image is displayed completely. but if I set the city, the interface updates, the picture changes and the UIImage itself is cropped as seen in the photo below

code of my constraints;

NSLayoutConstraint.activate([
            
            searchButton.widthAnchor.constraint(equalToConstant: 50),
            searchButton.heightAnchor.constraint(equalToConstant: 50),
            searchButton.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 30),
            searchButton.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 15),
            
            cityNameLabel.widthAnchor.constraint(equalToConstant: 200),
            cityNameLabel.heightAnchor.constraint(equalToConstant: 50),
            cityNameLabel.topAnchor.constraint(equalTo: searchButton.topAnchor),
            cityNameLabel.leadingAnchor.constraint(equalTo: searchButton.leadingAnchor, constant: 80),
            
            imageView.heightAnchor.constraint(equalToConstant: 170),
            imageView.widthAnchor.constraint(equalToConstant: 200),
            weatherStackView.topAnchor.constraint(equalTo: cityNameLabel.topAnchor, constant: 70),
            weatherStackView.leadingAnchor.constraint(equalTo: cityNameLabel.leadingAnchor)
        ])

I thought the problem was that the constraints of the view depend on the label, if the label changes, then the constraints also change. I did the exact dimensions of the label, but it didn't help

enter image description here

enter image description here

1

There are 1 best solutions below

0
ffb7c5 On

Looking at the two screenshots the frame size seems to be the same, supported by the fact you have a constant width anchor for the image view. I suspect the images you are using are of different sizes, if you don't want the image to be cropped you can set the imageView.contentMode = .scaleAspectFit but it will cause some extra white space on the top and bottom