0

我无法弄清楚为什么UIImagesUIImageViews的文件没有正确调整到UIImageView. 任何人都可以阐明我的代码中的问题吗?以下是来自 my 的相关代码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

4

2 回答 2

1

您可以尝试设置父视图的clipToBounds 吗?

如果图像视图在 self.view 中,则执行

 self.view.clipToBounds = YES;
于 2014-08-12T03:17:57.603 回答
0

不确定这会解决您的问题,但作为建议:

你不应该调用 [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);
        }];
    }


}
于 2014-08-12T04:08:57.487 回答