ios 如何在UIListContentConfiguration中获取符号图像的默认系统大小?

zf9nrax1  于 12个月前  发布在  iOS
关注(0)|答案(1)|浏览(107)

bounty已结束。回答此问题可获得+300声望奖励。赏金宽限期21小时后结束jvdveuten正在寻找一个从一个有信誉的来源回答developer.apple.com/documentation/uikit/…

developer.apple.com/documentation/uikit/…
我们该怎么用呢?它返回OP所说的,一个非常大的负数?
使用UIListContentConfiguration时,如果您提供SF符号image,系统将根据您的动态文字大小决定如何调整其大小。reservedLayoutSize文档指出:“符号图像将在预定义的reservedLayoutSize内居中,该reservedLayoutSize根据内容大小类别进行缩放。”另请注意,Mac Catalyst的图像大小由您在“系统偏好设置”>“通用”>"侧边栏图标大小“中的设置决定。
在我的应用程序中,我想创建一个像照片应用程序一样的带有单元格的侧边栏-一个相册单元格有一张照片作为图像,而不是一个符号。要做到这一点,我需要知道使图像的大小,以确保它的大小与其他单元格中的符号相同,因为单元格的图像视图的大小是由您提供的图像的大小决定的。“非符号图像将使用等于所显示图像实际大小的reservedLayoutSize。”
如何获得预定义的系统保留布局大小?
UIListContentConfiguration.ImageProperties.standardDimension听起来很有希望,但这只是一个通知系统使用其默认大小的常量,而不是暴露该大小-其值为-1.7976931348623157e+308。
contentConfiguration.imageProperties.reservedLayoutSize默认为CGSize.zero,这意味着使用默认大小。那么,我们如何才能达到这个规模呢?

mutmk8jj

mutmk8jj1#

根据文档正常使用:

您无法获取尺寸,但可以使用它来设置reservedLayoutSize的高度或宽度。然后,您必须根据当前文本大小调整图像的大小。这将考虑文本大小选项和辅助功能选项。这是正常的使用方法:

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

let font = UIFont.preferredFont(forTextStyle: .body)
let currentFontHeight = font.lineHeight

let originalImage = UIImage(named: "ImageNameInAsset")!
let scaleFactor = currentFontHeight / originalImage.size.height

let scaledImage = originalImage.resized(scaleFactor: scaleFactor)

var config = cell.defaultContentConfiguration()

config.image = scaledImage
config.text = "Text"

config.imageProperties.reservedLayoutSize.width = UIListContentConfiguration.ImageProperties.standardDimension
config.imageProperties.reservedLayoutSize.height = UIListContentConfiguration.ImageProperties.standardDimension
config.imageProperties.cornerRadius = 3

cell.contentConfiguration = config

return cell

取实际宽度和高度:

如果你想获取图像的实际宽度和高度并从那里开始工作,你必须解决UIListContentConfiguration.ImageProperties.standardDimension的奇怪行为,我创建了这个类来布局自定义图标图像,就像SF Symbol布局一样。
诀窍是在contentConfiguration中设置一个“clear”SF Symbol作为图像,然后设置一个自定义图像。然后,自定义图像符合UIListContentView内的imageLayoutGuide提供的约束,以匹配布局。它使自定义图像居中。
也许也可以创建一个自己的contentConfiguration类,但现在这对我来说很有效。希望它足够优雅能解决你的问题。它甚至可以根据不同的文本大小或可访问性选项进行缩放。
下面是如何使用它:

// In the tableview setup:
tableView.register(CustomImageViewCell.self, forCellReuseIdentifier: "customImageViewCell")

// When dequeuing a cell
let cell = tableView.dequeueReusableCell(withIdentifier: "customImageViewCell", for: indexPath) as! CustomImageViewCell

var config = cell.defaultContentConfiguration()

let imageConfig = UIImage.SymbolConfiguration.init(hierarchicalColor: .clear)
let image = UIImage(systemName: "calendar")?.withConfiguration(imageConfig)
config.image = image

config.text = "Label"

cell.contentConfiguration = config
                    
// This image will get overlayed in the custom class later on
cell.setCustomImage(UIImage(named: "YourAssetName"))

return cell

下面是自定义类:

class CustomImageViewCell: UITableViewCell {
    private let customImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.layer.cornerRadius = 3
        imageView.clipsToBounds = true
        return imageView
    }()
    
    private var didSetupConstraints = false
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        
        if !didSetupConstraints {
            if let contentView = contentView as? UIListContentView,
               let imageLayoutGuide = contentView.imageLayoutGuide {
                
                contentView.addSubview(customImageView)
                
                NSLayoutConstraint.activate([
                    customImageView.topAnchor.constraint(equalTo: imageLayoutGuide.topAnchor),
                    customImageView.bottomAnchor.constraint(equalTo: imageLayoutGuide.bottomAnchor),
                    customImageView.centerXAnchor.constraint(equalTo: imageLayoutGuide.centerXAnchor),
                    customImageView.widthAnchor.constraint(equalTo: imageLayoutGuide.heightAnchor)
                ])
                
                didSetupConstraints = true
            }
        }
    }
    
    func setCustomImage(_ image: UIImage?) {
        customImageView.image = image
    }
}

相关问题