Ios 何时/如何设置UIImageView内容模式(UIImageView设置内容模式不工作)

Ios 何时/如何设置UIImageView内容模式(UIImageView设置内容模式不工作),ios,objective-c,uiimageview,uiimage,contentmode,Ios,Objective C,Uiimageview,Uiimage,Contentmode,我不明白为什么我的UIImageView中的UIImages没有正确地调整到UIImageView的大小。有人能解释一下我代码中的问题吗?下面是myUICollectionViewCell的子类UICollectionViewCell中的相关代码。我正试图从显示UIImage(传单)的代码构建UICollectionViewCell,但无法使传单UIImage适应/调整到UIImageView的大小。我知道我必须设置contentMode,但无论我在哪里设置contentMode,图像都保持大小

我不明白为什么我的
UIImageView
中的
UIImages
没有正确地调整到
UIImageView
的大小。有人能解释一下我代码中的问题吗?下面是my
UICollectionViewCell
的子类
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 UpdateConstraint];在任何代码之后。作为一种最佳实践,您首先调用超级方法,然后按照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);
        }];
    }


}

我尝试添加self.clipsToBounds=是,但这并没有改变任何事情。(我已经将其添加到
UICollectionViewController
cellForItemAtIndexPath方法中。我甚至尝试删除
UICollectionViewController
中的cell.clipsToBounds,只将其放在
UICollectionViewCell
主体中,但这并没有改变任何事情。)您能否将imageView的clipToBounds也设置为YES?不幸的是,调用[\u flyerImageView SetClipToBounds:YES]不会改变任何东西。objc.io的(非常聪明的)作者会在调用super之前声明更新约束代码<代码>添加局部约束的位置是updateConstraints。确保在添加了布局子视图所需的任何约束后,在实现中调用[super UpdateConstraint]。