【问题标题】:How to use removefromsuperview()?如何使用 removefromsuperview()?
【发布时间】:2021-12-20 19:18:27
【问题描述】:

我想移除 UI 界面中的 self.view.subviews。

我尝试使用 view.removeFromSuperview(),它会删除整个 UI 视图。

【问题讨论】:

  • 你必须得到那个subview,一个你得到那个子视图的那个,比如说mySubView,然后你可以像mySubView.removeFromSuperview()一样使用这个属性。
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: ios swift3


【解决方案1】:

当您将视图添加为子视图时,给它一个标签,然后使用该标签删除该特定子视图:

// Adding subview on view
let subviewOne = UIView()
subviewOne.tag = 123
self.view.addSubview(subviewOne)

// Removing subview from view
// First find out the subview you want to remove

for subview in view.subviews {
    if subview.tag == 123 {
         subview.removeFromSuperview() // Remove the added subview
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 2012-08-13
    • 2011-01-01
    • 1970-01-01
    相关资源
    最近更新 更多