ios - iOS 11 中的 UITableViewCell 转换中断

标签 ios objective-c uitableview cgaffinetransform

在我构建的应用程序中,通过在 cellForRowAtIndexPath 中运行以下代码,我有一个表格 View ,在每个单元格的左侧显示图像:

cell.imageView.image = [UIImage imageNamed:myImage];

但是图片太大了,所以我把它缩小了:

cell.imageView.transform = CGAffineTransformMakeScale(0.3, 0.3);

这在 iOS 10 中工作得很好。但是一旦我升级到带有 iOS 11 SDK 的最新 Xcode,图像就会变得巨大。事实证明,转换 ImageView 的第二行代码现在什么也没做:我可以将其注释掉,将 0.3 更改为其他内容,等等,但没有任何区别。 CGAffineTransformMakeScale 在新的 Xcode 中仍然有文档,所以我假设它没有被弃用,但是为什么这会与 iOS 11 中断,我该如何修复它?有任何想法吗?提前致谢!请注意,我使用的是 Objective-C。

编辑:

刚刚尝试对代码进行 3 处更改:

  1. 将第二行更改为 cell.imageView.transform = CGAffineTransformMakeScale(0.0000001, 0.0000001);。没有任何反应(即 ImageView 和其中的图像仍然一样大)。

  2. 将第二行更改为 cell.imageView.transform = CGAffineTransformMakeScale(0, 0);。图像从 ImageView 中消失,但 ImageView 的大小仍然不变,您可以看出来,因为它仍然取代单元格中的文本并将其推到最右边。

  3. 删除第一行代码(不再将图像分配给 imageview)。 ImageView 消失,文本一直移动到单元格的左侧。

也许这可以帮助阐明正在发生的事情?

最佳答案

因此,如果您尝试调整图像大小以适应 imageView,您实际上应该像这样使用 imageView 的 contentMode 属性:

cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

或为其他人使用 Swift

cell.imageView.contentMode = .scaleAspectFit

这将保持图像的尺寸并将最大尺寸的图像放入 imageView 而不会改变尺寸

您也可以尝试 UIViewContentModeScaleAspectFit(或 Swift 中的 .scaleAspectFill),它基本上完全填充了 imageView 的尺寸,但是如果图片比 ImageView 更宽或更高,它会裁剪不适合的内容。

以下是直接来自 Obj-C 和 Swift 源文件的所有 contentModes:

typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
};

public enum UIViewContentMode : Int {
    case scaleToFill
    case scaleAspectFit // contents scaled to fit with fixed aspect. remainder is transparent
    case scaleAspectFill // contents scaled to fill with fixed aspect. some portion of content may be clipped.
    case redraw // redraw on bounds change (calls -setNeedsDisplay)
    case center // contents remain same size. positioned adjusted.
    case top
    case bottom
    case left
    case right
    case topLeft
    case topRight
    case bottomLeft
    case bottomRight
}

编辑:

因为我看到您也有兴趣更改 imageView 本身的尺寸(“为文本留出更多空间”)我建议的实际上是在 Storyboard 中或以编程方式使用AutoLayout 添加您自己的 imageView 并根据需要调整大小和位置,而不是在单元格上使用默认的 imageView ,这是一种方便/标准化的工具。

如果您对此不熟悉,我会在谷歌上搜索 AutoLayout 教程。或者“使用 AutoLayout 创建自定义 UITableViewCell”

如果您实际上不想创建自己的子类,您可以尝试在某处设置 `cell.imageView.frame = ..."以手动将其调整为您想要的大小,然后设置其内容模式以确保图像适合仍然很好。

看到这个问题:How do I make UITableViewCell's ImageView a fixed size even when the image is smaller

关于ios - iOS 11 中的 UITableViewCell 转换中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46985130/

相关文章:

ios - 如何从 UiTableViewController 访问自定义 UITableViewCell 上的属性?

iOS - 自定义 AccessoryView 未显示在 UITableViewCell 中

ios - swift 2.0,UITableView : cellForRowAtIndexPath returning nil for non-visible cells

iphone - Storyboard多个按钮导致相同的 segue

ios - 即使在 tableView reloadData 之后 UITableView 选择也会突出显示

iOS - 如何限制特定用户在不同设备上的应用程序安装

objective-c - 集合在枚举时发生了变异——归档数据并使用 NSCoder 写入文件

ios - 防止网站无响应时应用程序崩溃

ios - 使用 sprite 工具包的级别菜单的持久性

ios - 如何使 AVPlayerViewController 以编程方式进入全屏?