UIStackView 的 removeArrangedSubview 方法
当在 UITableViewCell 中使用了 UIStackView 时,在 prepareForReuse()
中调用了 UIStackView 的 removeArrangedSubview:
方法,发现 cell 重用时依然会显示错乱,查了一下文档,发现调用 removeArrangedSubview:
后,子 view 并不会从视图中移除,需要额外调用 removeFromSuperview()
方法。
Apple 官方文档:
This method removes the provided view from the stack’s
arrangedSubviews
array. The view’s position and size will no longer be managed by the stack view. However, this method does not remove the provided view from the stack’ssubviews
array; therefore, the view is still displayed as part of the view hierarchy.
To prevent the view from appearing on screen after calling the stack’sremoveArrangedSubview:
method, explicitly remove the view from the subviews array by calling the view’sremoveFromSuperview()
method, or set the view’sisHidden
property to true.