Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Aspect Fill Image Resizing with Content Aligned to Left/Top Edge

I am developing a universal app for iOS-7. this particular question is specific to iPad only. I want to place an image as background of root view of a View Controller i.e. the image should fill the whole screen. The 1x image has size:768x1024. This works for Portrait orientations as non-retina resolution of iPad is also 768x1024 in portrait. In Landscape however the image does not fit. I have tried using ScaleToFit but since aspect ratio of image is not preserved I can not use ScaleToFit(their are things in image which look odd when not scaled proportionally in both axis). AspectFill resizing seems most suitable for my need, but their is a small problem. As defined in Apple Documentation "The content is resized to completely fill the bounds rectangle, while still preserving the aspect of the content. The content is centered in the axis it exceeds.". I do not want the content to be centered in axis in which it exceeds, I want it to be aligned to top/left edge.

So basically I want two things:

  • Aspect Fill
  • The content remains aligned on the Left/Top edge.

Is it possible to achieve this. Any code-snippet will be great.

Thanks

like image 501
Taha Samad Avatar asked Nov 10 '22 08:11

Taha Samad


1 Answers

You will have to subclass the View Controller's view and manually scale and align.

Look at the code snippets in THIS answer.

Adjust imageViewXOrigin and imageViewYOrigin to align however you want.

like image 82
corro Avatar answered Nov 15 '22 05:11

corro