user1248568
user1248568

Reputation: 621

Disable UIImageView resizing behavior when contentMode UIViewContentModeScaleAspectFit

I have UIImageView with contentMode = UIViewContentModeScaleAspectFit

I need this mode to display my image properly. But also I need imageView cropping image when its frame changes like it was with UIViewContentModeTopLeft.

How I can obtain this?

Edited:

imageView.contentMode = UIViewContentModeTopLeft;
imageView.image = someImage;

Now I get this (and it's ok, image scaled properly and looks good):

enter image description here

Then, I have method:

- (void)setRating:(double)rating {
      double starWidth = imageView.image.size.width / _maxRating;
      CGRect oldFrame = self.frame;
      imageView.frame = CGRectMake(0, 0, rating * starWidth, oldFrame.size.height);
      _rating = rating;
}

after this method I need to get something like that:

enter image description here

BUT because of UIViewContentModeScaleAspectFit I get scaled image

Upvotes: 0

Views: 3148

Answers (1)

EricLeaf
EricLeaf

Reputation: 902

You can't really get what you want from that imageView in the method you are attempting without a lot of extra work. When you resize the view you will see that scaling, that is the mode you set and thats really all that will happen. There are a number of possibilities, many involve resizing things yourself thru subclassing. But as for solutions:

  1. Size your art prior to runtime, and then you don't need to do the scaling for every display of those stars for every user and every run. Save the electrons!
  2. Add the imageView as a subview to some other container view which you will match to the scaled size of the imageView, then resize to clip the imageView portion you do not want displayed.

Upvotes: 1

Related Questions