Skip navigation

I had a UIImageView control set up in one of my Views using Interface Builder, but I wanted to resize the UIImageView control programmatically depending on the device’s screen size.  I spent several hours trying all sorts of techniques, but I always had problems.  If I resized the UIImageView’s frame in viewDidLoad, it simply wouldn’t work.  If I resized its frame in viewDidAppear, it would work but the user would see the original-sized image before switching to the updated size which I felt was ugly and unnecessary.

I finally found a solution that would show the correctly resized UIImageView right away, but it required me to remove the UIImageView from my .XIB file, remove the outlet from Interface Builder, and remove the IBOutlet from my header file.  Then I simply programmatically added the UIImageView to the view in my viewDidLoad like so:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 173, 728, 771)];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [UIImage imageNamed:@"tutorial1.png"];
[self.view addSubview:imageView];

One Comment

  1. Did you try your original frame resize in viewWillAppear ?

    Because viewDidAppear gets called *after* the view is shown, as opposed to viewWillAppear which gets called *before* the view is shown and thus you wouldn’t get the ugly effect of seeing the original image size.

Leave a Reply

Your email address will not be published. Required fields are marked *