我无法弄清楚为什么我UIImages
中的UIImageViews
没有正确调整大小UIImageView
的大小。任何人都可以在我的代码中解释这个问题吗?以下是我SidewalkPlusSuperCell
的相关代码,它是一个子类UICollectionViewCell
。我正在尝试使用显示UICollectionViewCell
(传单)的代码构建UIImage
,但无法将传单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
当我运行代码时,我得到以下内容:
细胞应该是不同的大小(根据原始照片的大小来确定大小),但照片应该调整大小以适应细胞。尽管我设置了UIImages
?
UIViewContentMode
没有调整大小的任何想法
答案 0 :(得分:1)
您可以尝试设置父视图的clipToBounds吗?
如果图片视图在self.view中,那么
self.view.clipToBounds = YES;
答案 1 :(得分:0)
不确定这会解决您的问题,但建议:
你不应该调用[super updateConstraints];在任何代码之后。作为最佳实践,您首先调用超级方法然后键入代码,因为Apple文档指示在超级调用之前添加代码会导致意外行为。
- (void)updateConstraints
{
[super 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);
}];
}
}