从 UIScrollView 中删除所有子视图?
如何从 UIScrollview 中删除所有子视图?
How do I remove all of the subviews from a UIScrollview?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何从 UIScrollview 中删除所有子视图?
How do I remove all of the subviews from a UIScrollview?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
令
scrollView
为UIScrollView
的实例。在 Objective-C 中,这非常简单。只需调用
makeObjectsPerformSelector:
,如下所示:Objective-C:
在 Swift 中,您无法获得运行时访问权限,因此您必须自己实际处理迭代。
Swift:
一个简洁的版本,从这里:
一种更具描述性的方法来做到这一点(来自< a href="https://stackoverflow.com/a/27065076/224988">此处)假设
scrollview.subviews
:Let
scrollView
be an instance ofUIScrollView
.In Objective-C, it's pretty easy. Just call
makeObjectsPerformSelector:
, like so:Objective-C:
In Swift, you don't get that runtime access, so you have to actually handle the iteration yourself.
Swift:
A concise version, from here:
A more descriptive way to do this (from here) assumes
scrollview.subviews
:我认为你必须小心,不要删除滚动指示条。
Adam Ko 给出的代码简短而优雅,但它可能会删除水平和垂直滚动指示器。
我通常
假设你没有将 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
Suposing you don't have UIImageView's added to the scroll manually.
除了 Ricardo de Cillo 的之外,在我的例子中,我还有一个表视图,其中的子视图中有我想要删除的图像视图。
删除 !在 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.
The removal of the ! in the if command, and change
scrollview
toself.tableview
removed all images, but left the table view untouched.如果你想删除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.
补充之前答案中的 Swift 简洁版本(Swift 3.0 就绪):
Complementing the Swift concise version from a previous answer (Swift 3.0 ready):