I am programmatically setting up an imageview with a border. I set the content mode of the imageView to Aspect Fit, which works, but the border remains the original square.
Code:
CGRect imageViewRect = CGRectMake(left, top, 158, 119);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageViewRect];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setImage:image];
[imageView.layer setBorderColor:[[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth:3.0];
Obviously, I want the black border to adjust with the aspect fit and surround the image on all sides. Instead it remains in the original frame, and looks like this:

Using a method slightly modified from Frank Schmitt in this post, you can add a method to work out the frame needed to display the image scaled to aspect:
You can then call this after setting your UIImageView image:
Finally, set the UIImageView’s frame using this frame, offsetting position for any change in width/height: