UIStackView:是否真的需要同时调用removeFromSuperView和removeArrangedSubview来删除子视图? [英] UIStackView : Is it really necessary to call both removeFromSuperView and removeArrangedSubview to remove a subview?

查看:89
本文介绍了UIStackView:是否真的需要同时调用removeFromSuperView和removeArrangedSubview来删除子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIStackView类参考

在removeArrangedSubview中:

In removeArrangedSubview:

为防止在调用堆栈的removeArrangedSubview:方法后视图出现在屏幕上,请通过调用视图的removeFromSuperview方法从子视图数组中明确删除视图.

To prevent the view from appearing on screen after calling the stack’s removeArrangedSubview: method, explicitly remove the view from the subviews array by calling the view’s removeFromSuperview method.

在organizedSubview中:

In arrangedSubview:

每当调用排列视图的removeFromSuperview方法时,堆栈视图就会将该视图从其splittedSubview数组中删除

Whenever an arranged view’s removeFromSuperview method is called, the stack view removes the view from its arrangedSubview array

从这些看来,仅调用removeFromSuperview就足以删除一个子视图,而我一直在使用它而没有问题.我还通过调用removeFromSuperview时记录了rangingSubviews数组的计数来确认此行为.

From these, it seems that calling just removeFromSuperview is enough to remove a subview and I've been using it like that without problems. I also confirmed the behavior by logging the count of the arrangedSubviews array when removeFromSuperview is called.

但是,这里有许多有关S/O的教程和评论,请说两者都称呼.是否有一个原因?还是人们只是这样做,因为文档中是这样说的?

A lot of tutorials and comments here on S/O however, say to call both. Is there a reason for this? Or do people just do it because the documentation says so?

推荐答案

不,只需调用 subview.removeFromSuperview()

/* Removes a subview from the list of arranged subviews without removing it as
 a subview of the receiver.
    To remove the view as a subview, send it -removeFromSuperview as usual;
 the relevant UIStackView will remove it from its arrangedSubviews list
 automatically.
 */
open func removeArrangedSubview(_ view: UIView)

这篇关于UIStackView:是否真的需要同时调用removeFromSuperView和removeArrangedSubview来删除子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆