【问题标题】:Add Removed Subview Swift 3 iOS添加删除的子视图 Swift 3 iOS
【发布时间】:2016-12-22 10:25:19
【问题描述】:

在 self.view 中有一个子视图(tempView),我正在从 superview 中删除它,但是当我尝试将其添加回来时,它没有显示在视图中。在检查框架时,我发现他们的框架是正确的,但视图不可见。下面是从 superview 中删除我的视图并将其添加回来的代码。

if(self.view.subviews.contains(self.tempView))
{
    self.tempView.removeFromSuperview()
}
else
{
    self.view.addSubview(self.tempView)
    self.view.bringSubview(toFront:self.tempView)
    self.view.setNeedsLayout()
    self.view.layoutIfNeeded()
}

【问题讨论】:

  • 你还是需要重新设置框架
  • 如果你从超级视图中删除视图,那么你必须重新分配并重新构建它以添加
  • 如果视图对象设置为强则不需要。只有弱了才会失去属性。

标签: ios swift3 addsubview


【解决方案1】:

尝试设置 translatesAutoresizingMaskIntoConstraints=true

if(self.view.subviews.contains(self.tempView))
{
    self.tempView.removeFromSuperview()
}
else
{
    self.tempView.translatesAutoresizingMaskIntoConstraints=true
    self.view.addSubview(self.tempView)
    self.view.bringSubview(toFront:self.tempView)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2023-03-21
    • 2011-11-14
    • 1970-01-01
    • 2011-10-26
    • 2011-12-06
    • 1970-01-01
    相关资源
    最近更新 更多