Subview position when resizing aspect fill UIImageView
I'm trying to create layout of multiple UIImageViews that would retain
correct visual alignment after scaling. Eg.:
UIImageView *eyeView = [[UIImageView alloc] initWithImage:eyeImage];
eyeView.frame = CGRectMake(200, 250, 200, 200);
eyeView.contentMode = UIViewContentModeScaleAspectFill;
eyeView.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleLeftMargin;
UIImageView *faceView = [[UIImageView alloc] initWithImage:faceImage];
faceView.frame = CGRectMake(0, 0, 1000, 1000);
faceView.contentMode = UIViewContentModeScaleAspectFill;
[self.faceView addSubview:eyeView];
Then I add it to target view which has dimensions of applicationFrame (eg.
480x300):
[self.handsetView addSubview:faceView];
The problem is that eyeView is mispositioned relatively lower than it
should. I suspect that it is due to UIViewAutoresizingFlexibleTopMargin
and UIViewContentModeScaleAspectFill not working nicely together:
Layout is transformed correctly if i use UIViewContentModeScaleToFill on
faceView. Is it possible to create such a layout with autolayout
constraints or should I use different approach? Could you please point me
in right direction?