[英]how to set the UIImageView align left or right when set the contentMode to UIViewContentModeScaleAspectFit?
[英]When/How to set UIImageView contentMode (UIImageView setContentMode not working)
我無法弄清楚為什么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
大小的想法?
您可以嘗試設置父視圖的clipToBounds嗎?
如果圖像視圖在self.view中,則執行
self.view.clipToBounds = YES;
不確定是否可以解決您的問題,但建議您:
您不應該調用[super updateConstraints]; 在任何代碼之后。 作為最佳實踐,您首先調用超級方法,然后鍵入代碼,因為蘋果文檔指示在超級調用可能導致意外行為之前添加代碼。
- (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);
}];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.