swift - 如何在启用 translatesAutoresizingMaskIntoConstraints 的情况下布局视图
问题描述
据我了解,有一种新方法可以为 iOS 14 配置 UITableView 单元格。我正在使用这种方法,但是当我实现它时,出现了错误。
Exception NSException * "The content view returned from the content configuration must have translatesAutoresizingMaskIntoConstraints enabled"
但是,当我设置translatesAutoresizingMaskIntoConstraints
为 true 时,我无法再为单元格布局视图。也就是说,即使我设置了视图的宽度,也没有显示任何内容。
下面是我正在使用的方法(没有配置视图的代码)。我的问题是是否有办法在仍然能够布局视图的同时避免错误。
@available(iOS 14, *)
extension GoalCell {
struct Configuration: UIContentConfiguration {
func makeContentView() -> UIView & UIContentView {
return MyContentView(configuration: self)
}
func updated(for state: UIConfigurationState) -> GoalCell.Configuration {
return self
}
}
class MyContentView: UIView, UIContentView {
var configuration: UIContentConfiguration {
didSet {
self.configure()
}
}
}()
init(configuration: UIContentConfiguration) {
self.configuration = configuration
super.init(frame: .zero)
self.configure()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func configure() {
guard let config = self.configuration as? Configuration else { return }
self.translatesAutoresizingMaskIntoConstraints = true
}
}
解决方案
推荐阅读
- c - 如何使用赋值(=)和相等(==)操作数制作语法?
- python - 按下按钮并根据您在下拉菜单中的选择执行某些操作?
- vba - 有没有办法在附件内容之后动态重命名传入电子邮件的主题行?
- python - 在 Python 中运行 Theil-Sen 回归时出错
- c# - 向 .NET Core 应用程序添加系统引用
- dns - 具有权威位集(或用于响应的其他位)的 DNS 查询是否被视为有效?
- google-cloud-platform - 自定义域下谷歌云存储下载速度慢
- python - Google Cloud Composer 安装依赖项的时间过长
- python - Kivy 'UrlRequest' 无法获取 json
- python - 使用 pip 找不到命令 python2.7