I've got a viewcontroller I'm using to load an image from the web. The image is usually going to be much larger than the viewport so I'm scaling it down and placing it in a UIImageView. The problem is it doesn't want to align to the top of the viewport. How would I fix this. Here's the code:
- (void)viewDidLoad
{
NSURL *imageURL = [NSURL URLWithString:[[self exhibit] image]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *iv = [[UIImageView alloc] initWithImage:image];
[iv setContentMode:UIViewContentModeScaleAspectFit];
[iv setTranslatesAutoresizingMaskIntoConstraints:NO];
[[self view] addSubview:iv];
[self setImageView:iv];
NSDictionary *nameMap = @{@"imageView" : [self imageView]};
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[imageView]-10-|"
options:NSLayoutFormatAlignAllCenterX
metrics:nil
views:nameMap];
[[self view] addConstraints:horizontalConstraints];
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[imageView]"
options:NSLayoutFormatAlignAllTop
metrics:nil
views:nameMap];
[[self view] addConstraints:verticalConstraints];
NSLog(@"%@", NSStringFromCGRect([[self imageView] frame]));
}
The log at the end shows that the UIImageView is the size of the original image size not the scaled down version. I'm assuming this is the problem, but I'm not sure how to fix it.