Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Resizing UIImageView with AutoLayout

I am new to AutoLayout and struggling with it a bit. I have a UIImageView which is part of an UIView. When the app is running on a 3.5 inch device, I want the UIImageView to resize according to the change of the screen size.

I am not sure how to achieve this. From what I have seen so far, you can only set a fixed width and height of the views, is it possible to make them dynamically resize ? In other words, how can the UIImageView resize accordingly to the super view's height ?

like image 995
Petar Avatar asked Dec 25 '22 14:12

Petar


2 Answers

  1. Choose your ImageView in Interface Biulder.
  2. Press pin button.
  3. Set the constraints.

Example enter image description here

like image 149
12pm Avatar answered Jan 09 '23 13:01

12pm


This code will size the UIImageView according to its superview's width and height.

UIImageView *myImageView = [[UIImageView alloc] initWithImage:theImage];
[self.view addSubview:myImageView];

[myImageView setTranslatesAutoresizingMaskIntoConstraints:NO];

NSArray *imageViewConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[myImageView]|" options:0 metrics:nil views:@{@"myImageView": myImageView}];
[self.view addConstraints:imageViewConstraints];

imageViewConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[myImageView]|" options:0 metrics:nil views:@{@"myImageView": myImageView}];
[self.view addConstraints:imageViewConstraints];
like image 32
Eric Avatar answered Jan 09 '23 13:01

Eric