固定cell.imageView.image的大小

本文探讨了如何解决iOS Cell imageView图片大小随Cell高度变化的问题,并提供了多种解决方案,包括设置imageview的size、使用UIGraphics进行绘图、自定义cell等方法。重点介绍了简单方便的方法及几种未能成功的尝试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cell.imageView.image的大小 会随着Cell的高度而变化,不同的图片显示的也不一样,在网上找了几种方法,简单方便的是下面这种:

UIImage *icon = [UIImage imageWithName:@"xxxxx"];
CGSize itemSize = CGSizeMake(30, 30);
UIGraphicsBeginImageContextWithOptions(itemSize, NO,0.0);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[icon drawInRect:imageRect];
 
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

另外还有其它方法没有成功:设置cell.imageView.contentMode的属性,
还有一种是网上转载的比较多的一种,由于不是自定义的cell,没用到此方法 :

重定义一个uitableviewcell,它自动生成的代码不需要动,只要在.m文件中再加上下面的代码即可:

(具体可参看:http://blog.youkuaiyun.com/zhangjie1989/article/details/7550990)

- (void)layoutSubviews {

    [superlayoutSubviews];

    self.imageView.bounds =CGRectMake(0,0,44,44);

    self.imageView.frame =CGRectMake(0,0,44,44);

    self.imageView.contentMode =UIViewContentModeScaleAspectFit;

    

    CGRect tmpFrame = self.textLabel.frame;

    tmpFrame.origin.x = 46;

    self.textLabel.frame = tmpFrame;

    

    tmpFrame = self.detailTextLabel.frame;

    tmpFrame.origin.x = 46;

    self.detailTextLabel.frame = tmpFrame;    

}


还有一种最直接 的方法 就是自定义一个cell。


您可以按照以下代码来进行布局: ```swift import SnapKit class YourTableViewCell: UITableViewCell { private let label = UILabel() private let imageView = UIImageView() override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) // Add subviews to contentView contentView.addSubview(label) contentView.addSubview(imageView) // Set label properties label.numberOfLines = 0 // Set constraints for label label.snp.makeConstraints { make in make.top.equalToSuperview().offset(5) make.left.right.equalToSuperview() } // Set constraints for imageView imageView.snp.makeConstraints { make in make.top.equalTo(label.snp.bottom).offset(10) make.left.right.equalToSuperview() make.bottom.equalToSuperview().offset(-10) make.height.equalTo(0) } } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func configure(with text: String, image: UIImage?) { label.text = text // Update constraints for imageView based on the presence of an image if let image = image { imageView.image = image imageView.snp.updateConstraints { make in make.height.equalTo(50) make.top.equalTo(label.snp.bottom).offset(10) make.bottom.equalToSuperview().offset(-10) } } else { imageView.image = nil imageView.snp.updateConstraints { make in make.height.equalTo(0) make.top.equalTo(label.snp.bottom).offset(0) make.bottom.equalToSuperview().offset(-10) } } } } ``` 在上述代码中,我们使用了 `snp` 的链式语法来设置布局。我们首先初始化了一个 `UILabel` 一个 `UIImageView` 并将它们添加到 `contentView` 上。然后,我们设置了 `UILabel` 的一些属性,如 `numberOfLines` 来使其多行显示。接着,我们设置了 `UILabel` `UIImageView` 的约束。在 `configure(with:image:)` 方法中,我们根据传递进来的 `image` 参数来更新 `UIImageView` 的约束。如果 `image` 不为 `nil`,则我们将 `UIImageView` 的高度设置为 50,并将其放置在 `UILabel` 的下方。如果 `image` 为 `nil`,则我们将 `UIImageView` 的高度设置为 0,使其不可见,并将其放置在 `UILabel` 的正下方。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值