我不明白为什么我的 UIImageViews
中的 UIImages
没有正确地调整到 UIImageView
的大小。任何人都可以阐明我的代码中的问题吗?以下是我的 SidewalkPlusSuperCell
的相关代码,它是一个子类 UICollectionViewCell
。我正在尝试从显示 UIImage
(传单)的代码构建 UICollectionViewCell
,但无法使传单 UIImage
适合/调整大小UIImageView
的大小。我知道我必须设置 contentMode
,但无论我在哪里设置 contentMode
,图像都保持未调整大小。
//
// 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
当我运行代码时,我得到以下信息:
单元格应该有不同的大小(根据原始照片的长宽比调整大小),但照片应该调整大小以适合单元格。有没有想过为什么尽管我设置了 UIViewContentMode
,但 UIImages
没有调整大小?
最佳答案
您可以尝试设置父 View clipsToBounds 吗?
如果image view在self.view什么时候做
self.view.clipToBounds = YES;
关于ios - 何时/如何设置 UIImageView contentMode(UIImageView setContentMode 不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25255487/