I cannot figure out why the UIImages
in my UIImageViews
are not properly resizing to the size of the UIImageView
. Can anyone shed some light on the problem in my code? The follow is the relevant code from my SidewalkPlusSuperCell
, which is a subclassed UICollectionViewCell
. I am trying to construct a UICollectionViewCell
from code that displays a UIImage
(a flyer), but cannot get the flyer UIImage
to fit/resize to the size of the UIImageView
. I know that I have to set the contentMode
, but no matter where I seem to set the contentMode
, the images remain unsized.
//
// SidewalkPlusSuperCell.m
//
#import "SidewalkPlusSuperCell.h"
#import "Masonry.h"
@interface SidewalkPlusSuperCell()
@property (nonatomic) BOOL showInformation;
@property (strong, nonatomic) UIImageView *flyerImageView;
@end
@implementation SidewalkPlusSuperCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_showInformation = NO;
}
return self;
}
- (void)updateConstraints
{
if (_showInformation) {
UIEdgeInsets imagePlusTextPadding = UIEdgeInsetsMake(5, 5, 5, 5);
[_flyerImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.superview.mas_top).with.offset(imagePlusTextPadding.top);
make.leading.equalTo(self.superview.mas_leading).with.offset(imagePlusTextPadding.left);
make.bottom.equalTo(self.superview.mas_bottom).with.offset(imagePlusTextPadding.bottom);
}];
} else {
UIEdgeInsets onlyImagePadding = UIEdgeInsetsMake(0, 0, 0, 0);
[_flyerImageView mas_updateConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.superview).with.insets(onlyImagePadding);
}];
}
[super updateConstraints];
}
- (void)drawRect:(CGRect)rect
{
if (_showInformation) {
} else {
_flyerImageView = [[UIImageView alloc] init];
[_flyerImageView setFrame:rect];
[_flyerImageView setContentMode:UIViewContentModeScaleAspectFit];
[_flyerImageView setImage:self.flyerImage]; //self.flyerImage is a public property that is set by the UICollectionViewController
[self addSubview:_flyerImageView];
UIEdgeInsets onlyImagePadding = UIEdgeInsetsMake(0, 0, 0, 0);
[_flyerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.superview).with.insets(onlyImagePadding);
}];
[self updateConstraints];
}
}
@end
When I run the code, I get the following:
The cells are supposed to be different sizes (sized based on aspect of the original photo), but the photos are supposed to resize to fit the cells. Any thought as to why the UIImages
are not resizing despite me setting the UIViewContentMode
?
Not sure this will solve your issue but as a recommendation:
You shouldn't call [super updateConstraints]; after any code. As a best practice you call super methods first then you type your code as apple documentation indicates to add code before the super invocation can cause unexpected behaviour.