IOS: resizing UIImageView using AutoLayout

I am new to AutoLayout and struggling with it a bit. I have UIImageViewwhich is part of the UIView. When the application runs on a 3.5-inch device , I want the UIImageView to resize to fit the screen.

I am not sure how to achieve this. From what I have seen so far, you can only set the fixed width and height of the views, is it possible to make them dynamically resized ? In other words, how UIImageViewdoes the size change to fit the height of the super view?

+3
source share
4 answers
  • Select your ImageView in the Biulder interface.
  • .
  • .

enter image description here

+7

UIImageView .

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];
+3

I had the same problem too. It might help someone

If you are designing in an InterfaceBuilder or Storyboard, simply attach all the edges to the Supervision (ie) Lead Space, Trailing Space, Top Space and Bottom Spoace to the UIView. What he

+1
source

select an image. Now go ahead and add a pin with a lower spacing from the super view.

-1
source

All Articles