【问题标题】:How to remove a UIButtons placed into scrollview via a for loop when a new array is chosen to be placed into that scroll view?如何在选择新数组被播放到该滚动视图时,如何删除将放置到滚动视图中的UIBUtonons?
【发布时间】:2017-01-12 19:32:49
【问题描述】:

我需要一点帮助。根据某人可以扔飞盘的距离,我通过滑块使用该值将我给它们的建议数组复制到 arrayToUse 变量中,该变量创建 UIButtons 带有我水平放置在底部的图像VC 使用滚动视图。用户可以再次移动滑块以选择不同的距离并点击提交,它将根据新距离加载不同的数组。我的问题是,如果他们选择了一个新数组,其中的图像少于他们开始使用的图像,我不知道如何关闭以前的图像。我可以在位于顶层下方的视图调试层次结构中看到它,并且它仍然会显示好像有更多图像可以在滚动视图的末尾滚动到,因为下面有更长的数组。有没有办法可以在他们点击提交按钮时关闭前一个数组并加载一个新数组?

for i in 0..<arrayToUse.count {

    let imageView = UIButton ()
    imageView.setImage(arrayToUse[i].image, for: .normal)
    imageView.contentMode = .scaleAspectFit
    let xPosition = (self.view.frame.width - 60) * CGFloat(i)

    imageView.frame = CGRect(x: xPosition + 30, y: view.frame.height - self.scrollView.frame.width + 30, width: self.scrollView.frame.width - 60 , height: self.scrollView.frame.width - 60)

    scrollView.contentSize.width = (scrollView.frame.width * CGFloat(i + 1)) - (CGFloat(60 * i))
    scrollView.addSubview(imageView)

    imageView.addTarget(self, action: #selector(discSelectionVC.buttonTapped(_:)), for: .touchUpInside)
    imageView.tag = i

}

【问题讨论】:

    标签: ios arrays swift3 xcode8


    【解决方案1】:

    如果我没听错的话,我认为这就是你想要的:

    for subview in scrollView.subviews {
    
        if subview is UIButton {
            // Perform logic here to determine if you want to remove the UIButton from the view
            subview.removeFromSuperview()
        }
    }
    

    这将遍历你的 scrollView 中的子视图,然后如果它发现子视图是 UIButton,那么根据你编写的逻辑,你可以选择删除或不删除该 UIButton。

    【讨论】:

    • 我没有在您提供的代码中添加任何额外的逻辑,它完全符合我的期望。竖起大拇指先生。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 2015-03-18
    相关资源
    最近更新 更多