iosuiimageviewuiviewcontentmode

Align UIImageView with contentMode AspectFill AND Top


When I use AspectFill, the image will be centered in image view, top and bottom are cut off. But I want it to start from top, not center. I see many questions but they're all about AspectFit, not AspectFill. Is there anyway to apply both AspectFill and Top to contentMode of UIImageView?


Solution

  • I've just found this UIImageViewAligned and decided to answer my own question. It works perfectly.

    It is a subclass of UIImageView that allows you to customize the alignment of the displayed image inside the view's frame. This works even if the contentMode is set to AspectFit, AspectFill or ScaleToFill.