【发布时间】:2017-02-26 15:33:06
【问题描述】:
当视图消失时,我从超级视图中删除了这个collectionview,因为内存问题,但是我想在视图出现时添加它,我只想添加它!还是有其他方法可以做到这一点?谢谢。
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
let subViews = self.view.subviews
for subview in subViews{
if subview.tag == 1 {
subview.removeFromSuperview()
}
}
}
更新
var savedView: UIView?
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
let subViews = self.view.subviews
for subview in subViews{
if subview.tag == 1 {
savedView = subview
subview.removeFromSuperview()
}
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let subview = savedView {
view.addSubview(subview)
savedView = nil
}
}
但它不会将其添加回来。
【问题讨论】:
-
您确定该子视图的标记为 1,并且该子视图正在被适当地删除,并且 saveView 在保存后不是 nil 吗?
-
是的,collectionview 已被删除,但我无法将其添加回来。当我 print() 视图中的子视图确实消失时,collectionview 被保存。
-
这是
UICollectionViewController吗? -
不,不是。如果我从代码创建collectionview,它会起作用吗?我可以在 viewWillAppear 中创建它并在 viewWillDisappear 中删除它。
标签: swift swift3 uicollectionview