如何解决SwiftUI:当我从视图层次结构中删除受控视图时,是否需要从控制器链中删除 UIHostingController?
我正在尝试将我的应用程序中的 UIKit 视图迁移到 SwiftUI。我的应用程序中的核心元素之一是 UICollectionView。我正在使用 UIHostingController 嵌入 SwiftUI 视图 - 到目前为止一切顺利。
我想知道,既然我的单元格是可重复使用的,那么当单元格被回收时 UIHostingController 会发生什么? 我需要将它从控制器链中取出吗? 如果需要,最好的方法是什么? (将 UIHostingController 存储在单元格中?)
例如。到目前为止,标题视图如下所示:
class HeaderViewCell: UICollectionReusableView {
var layoutAttributes:GroupHeaderViewLayoutAttributes = GroupHeaderViewLayoutAttributes()
public func attachContent(model:GroupHeaderModel,controller:UIViewController){
let view = GroupHeaderView(model: model,layoutAttributes: self.layoutAttributes)
let hostingController = UIHostingController(rootView: view)
if let contentView = hostingController.view {
controller.addChild(hostingController)
self.addSubviewAndConstrains(contentView)
}
}
override func prepareForReuse() {
self.subviews.forEach { $0.removeFromSuperview() }
layoutAttributes = GroupHeaderViewLayoutAttributes()
}
override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
if let attributes = layoutAttributes as? TileViewLayout.HeaderViewAttributes {
self.layoutAttributes.topBarHeight = attributes.topBarHeight
self.layoutAttributes.indicatorWidth = attributes.indicatorWidth
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。