从 UIScrollView 中删除所有子视图?

发布于 2024-09-30 21:15:43 字数 33 浏览 8 评论 0原文

如何从 UIScrollview 中删除所有子视图?

How do I remove all of the subviews from a UIScrollview?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧人九事 2024-10-07 21:15:43

scrollViewUIScrollView 的实例。

在 Objective-C 中,这非常简单。只需调用 makeObjectsPerformSelector:,如下所示:

Objective-C:

[scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

在 Swift 中,您无法获得运行时访问权限,因此您必须自己实际处理迭代。

Swift:

一个简洁的版本,从这里

scrollview.subviews.map { $0.removeFromSuperview() }

一种更具描述性的方法来做到这一点(来自< a href="https://stackoverflow.com/a/27065076/224988">此处)假设 scrollview.subviews

let subviews = self.scrollView.subviews
for subview in subviews{
    subview.removeFromSuperview()
}

Let scrollView be an instance of UIScrollView.

In Objective-C, it's pretty easy. Just call makeObjectsPerformSelector:, like so:

Objective-C:

[scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

In Swift, you don't get that runtime access, so you have to actually handle the iteration yourself.

Swift:

A concise version, from here:

scrollview.subviews.map { $0.removeFromSuperview() }

A more descriptive way to do this (from here) assumes scrollview.subviews:

let subviews = self.scrollView.subviews
for subview in subviews{
    subview.removeFromSuperview()
}
血之狂魔 2024-10-07 21:15:43

我认为你必须小心,不要删除滚动指示条。

Adam Ko 给出的代码简短而优雅,但它可能会删除水平和垂直滚动指示器。

我通常

for (UIView *v in scrollView.subviews) {
  if (![v isKindOfClass:[UIImageView class]]) {
    [v removeFromSuperview];
  }
}

假设你没有将 UIImageView 手动添加到滚动中。

I think you just have to be careful and not to delete the scroll indicator bars.

The code given by Adam Ko is short and elegant but it may delete the horizontal and vertical scroll indicators.

I usually do

for (UIView *v in scrollView.subviews) {
  if (![v isKindOfClass:[UIImageView class]]) {
    [v removeFromSuperview];
  }
}

Suposing you don't have UIImageView's added to the scroll manually.

≈。彩虹 2024-10-07 21:15:43

除了 Ricardo de Cillo 的之外,在我的例子中,我还有一个表视图,其中的子视图中有我想要删除的图像视图。

for (UIView *v in self.tableview.subviews) {
  if ([v isKindOfClass:[UIImageView class]]) {
    [v removeFromSuperview];
  }
}

删除 !在 if 命令中,将 scrollview 更改为 self.tableview 删除了所有图像,但保持表格视图不变。

In addition to Ricardo de Cillo's, in my case I had a table view that had imageviews in the subviews that I wanted to remove.

for (UIView *v in self.tableview.subviews) {
  if ([v isKindOfClass:[UIImageView class]]) {
    [v removeFromSuperview];
  }
}

The removal of the ! in the if command, and change scrollview to self.tableview removed all images, but left the table view untouched.

独闯女儿国 2024-10-07 21:15:43

如果你想删除scrollview.subviews中的uiimageviews,并且你还想保留垂直和水平指示器。您可以设置一个特殊的“标签”来标识视图,并排除标签默认为0的垂直和水平指示器。

If you want to remove uiimageviews in scrollview.subviews, and you also want to keep the vertical and horizontal indicators. You can set a special "tag" to identify views and exclude vertical and horizontal indicators whose tags are 0 by default.

感性 2024-10-07 21:15:43

补充之前答案中的 Swift 简洁版本(Swift 3.0 就绪):

_ = scrollView.subviews.filter { $0 is UIImageView }.map { $0.removeFromSuperview() }

Complementing the Swift concise version from a previous answer (Swift 3.0 ready):

_ = scrollView.subviews.filter { $0 is UIImageView }.map { $0.removeFromSuperview() }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文