Taha Samad
Taha Samad

Reputation: 1145

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:

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

Thanks

Upvotes: 5

Views: 2072

Answers (2)

corro
corro

Reputation: 363

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.

Upvotes: 1

InkGolem
InkGolem

Reputation: 2762

Really the only way to get around the content fill mode is to have two different images, one for each orientation. I'd suggest changing the image in the view controllers willAnimateToOrientation: method so that you can put the image changes inside UIKit's animation block.

Upvotes: 0

Related Questions