【问题标题】:How to remove only user-added subviews from my UIView如何从我的 UIView 中仅删除用户添加的子视图
【发布时间】:2015-01-29 23:50:39
【问题描述】:

我正在尝试删除我添加到视图中的所有子视图,因此我实现了一个循环来迭代子视图,如下所示:

for subview in view.subviews {
    println(subview)
    //subview.removeFromSuperview()
}

我通过在我的视图中添加 UILabel 进行了测试,然后运行了这段代码。输出包含我的 UILabel,但也包含 _UILayoutGuide。那么,我的问题是如何确定子视图是我添加的还是系统添加的?

【问题讨论】:

  • 如果你在这个答案中指定子视图类型怎么办:stackoverflow.com/a/25067739/2274694
  • @LyndseyScott 我不确定这对我有什么帮助
  • 无论哪种方式,您要删除特定类型的视图,即 UIViews、UILabels 等?因为您可以按类别限制子视图的删除...我可以为您输入答案。
  • 您可以添加自己的子视图并将标签放在那里,而不是在view 中工作。然后,您可以安全地删除该视图的所有子视图。
  • 实际上,我最终提出了一些更彻底的建议。查看我的更新答案。

标签: ios swift uiview subviews


【解决方案1】:

如果您只想阻止循环删除 _UILayoutGuide(属于 UILayoutSupport 类),请尝试以下操作:

for subview in self.view.subviews {
    if !(subview is UILayoutSupport) {
        print(subview)
        subview.removeFromSuperview()
     }
}

一般来说,如果您想阻止删除 _UILayoutGuide 以外的视图,并且如果您知道要从 UIView 中删除的特定类型的子视图,则可以限制您删除的子视图对于这些类型,例如:

for subview in view.subviews {
    if subview is ULabel {
        println(subview)
        subview.removeFromSuperview()
    }
}

【讨论】:

    【解决方案2】:

    一种选择是给你添加的所有视图一个特定的标签。然后只有当他们有那个标签时才删除它们。

    userCreatedView.tag = 100;
    

    ...

    for subview in view.subviews {
        if (subview.tag == 100) {
            subview.removeFromSuperview()
        }
    }
    

    您还可以保留用户添加的所有子视图的数组,然后检查该子视图是否在您的 userAddedViewsArray 中

    或者您可以为用户添加的视图创建 UIView 的子类,然后只删除属于该类的子视图

    【讨论】:

    • 谢谢。不幸的是,我已经将 tag 属性用于其他用途。
    • 刚刚添加了几个其他选项。有很多方法可以做到这一点,而且都很简单。
    • 我希望避免在每次添加新子视图时添加额外的代码。不过,感谢您的好建议。
    • 标签属性救了我,谢谢
    【解决方案3】:

    我这样解决问题.... 首先我添加了子视图

    • [self.view addSubview:genderPicker];

    然后从视图中仅删除该子视图

    • [genderPicker removeFromSuperview];

    【讨论】:

      【解决方案4】:

      通过字符串输入乐趣来关闭指示器 swift 2.2 及以上版本

      func activityonoff(viewcontroler:UIViewController,string:String){
      let container: UIView = UIView()
      let loadingView: UIView = UIView()
      if string == "on"{
      container.frame = viewcontroler.view.frame
      container.center = viewcontroler.view.center
      container.backgroundColor = UIColor.whiteColor()
      container.alpha = 1
      container.tag = 1
      
      loadingView.frame = CGRectMake(0, 0, 80, 80)
      loadingView.center = container.center
      loadingView.backgroundColor = UIColor(red: 4/255, green: 68/255, blue: 68/255, alpha: 0.7)
      loadingView.clipsToBounds = true
      loadingView.layer.cornerRadius = 10
      
      let actInd: UIActivityIndicatorView = UIActivityIndicatorView()
      actInd.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
      actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
      actInd.center = CGPointMake(loadingView.frame.size.width / 2,loadingView.frame.size.height / 2);
      loadingView.addSubview(actInd)
      container.addSubview(loadingView)
      viewcontroler.view.addSubview(container)
      actInd.startAnimating()
      }
      else{
          for v in viewcontroler.view.subviews{
              if v.tag == 1{
                  v.removeFromSuperview()
              }
          }
      
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-07
        • 1970-01-01
        • 2017-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-29
        相关资源
        最近更新 更多