For the purity of the experiment, I created a new project, and in the StoryBoard in the ViewController I threw the Image View, I set the picture there, and also the dimensions 200x200. And hooked it up in the code. Actually, this is the controller code itself:

import UIKit class ViewController: UIViewController { @IBOutlet weak var testIMG: UIImageView! override func viewDidLoad() { super.viewDidLoad() testIMG.frame = CGRectMake(300, 150, 10, 10) } } 

It seems that the dimensions should change, but when you start the application, the picture remains the same size. And no matter what size I put in the code (at least 100x100, at least 10x10), all to no purpose, the picture remains 200x200.

Tell me what am I doing wrong?

  • try moving your code to viewDidAppear - Max Mikheyenko
  • @MaxMikheyenko did not help. I even tried instead of resizing, programmatically change the image: testIMG.image = UIImage (named: "foo"), but also no changes - neither in viewDidLoad nor in viewDidAppear. - cheerful_weasel
  • then your viewController has a function -(void)viewDidLayoutSubviews here it will work exactly - Max Mikheyenko
  • @MaxMikheyenko doesn't work anyway ((( cheerful_weasel
  • discard all code, take a look - Max Mikheyenko

2 answers 2

Try to remove the constant dimensions in the Main.storyboard . Instead of the sizes put bottom and top constraints .

Or create a UIImageView from code, not storyboard.

  • Did not quite understand what you mean. How to remove constant sizes? If you remove the dimensions, then it turns out just 0x0. And if you put the constraints for the top and bottom, it simply stretches to the full height of the screen. - cheerful_weasel
  • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky
  • Yes, from the code to create UIImageView it turns out. And also I set the height of the UIView (to which the pictures are added) with the help of NSLayoutConstraint by the parameter .Height - cheerful_weasel

In the interface builder for the UIImageView that you add, set the mode attribute as the aspect fit may help. And read this article.

  • There is still a possibility that you are crookedly setting the constraints, so the size does not change - G.Renat
  • Not aspect fit does not help. I went the other way. First, now I create a UIImageView from the code and add it to the previously prepared UIView. Secondly, I use the constraint with the .Height parameter to set the height of my view. Now everything works. - cheerful_weasel