Search code examples
objective-cuiviewios6uiimageviewautolayout

UIImageView within UIView doesn't respect constraint


I have a UIView of size width: 285 and height: 243. Within that view, I have a UIImageView (it is within that UIView according to Storyboard). That UIImageView has width 283 and height 241 (so that the UIView shows 1px around). I position that UIImageView 1px vertically and 1px horizontally from the UIView. Now, for some reason, everything shows fine within Storyboard, but once it runs, the UIImageView doesn't respect any of the height or width I gave. IF I set the mode to Scale to fill, it works, but I want Aspect fill which it doesn't work.

I am using auto-layout by the way. Any ideas?!


Solution

  • You haven't said what about it "works" so I'm going to guess.

    Aspect fit, by its definition, will likely show a different border on 2 edges because it has resized the image for display. If your aim is to put a border around the image then you should:

    1. Remove the container view (it offers no benefit)
    2. Add a border to the image views layer
    3. Calculate the aspect ratio of the image
    4. Set the image view frame size to fit around the image

    A number of options are described in this post: How to scale a UIImageView proportionally?.