Friday, September 16, 2011

Lock Aspect Ratio to Max

This is pretty much exactly how UIImageView works when you use "Aspect Fill." If you use this in the enclosing view's setFrame, you can let the UIImageView freewheel (scale to fit), and resize other things according to your new size:

-(void)setFrame:(CGRect)frameIn {
    if (!ignoreAspectRatio) {
        float aspectRatio = self.frame.size.width / self.frame.size.height;
        frameIn.size.width = MIN(frameIn.size.width, frameIn.size.height * aspectRatio);
        frameIn.size.height = frameIn.size.width/aspectRatio;
    }
    [super setFrame:frameIn];
}

This kind of programming is not my strong suit, but with a few minutes goofing around in Excel, it was easy to figure out. And it works.

No comments: