我在IB中有一个UIStackView,我正在viewDidLoad中删除并添加子视图。删除子视图时,它会崩溃。
[self.headerStackView.arrangedSubviews each:^(UIView *subview) {
[self.headerStackView removeArrangedSubview:subview];
[subview removeConstraints:subview.constraints];
[subview removeFromSuperview];
}];
调试器:
2016-11-29 12:35:18.568137 RocheUnregulated[2211:937474] [LayoutConstraints] View hierarchy unprepared for constraint.
Constraint: <NSLayoutConstraint:0x17429ec80 'UISV-spacing' H:[EntryItemInfoView:0x10fe10840]-(15)-[EntryItemInfoView:0x10fd85e50] (active)>
Container hierarchy:
<UIStackView: 0x10fe14160; frame = (125 12; 183 51); opaque = NO; autoresize = RM+BM; layer = <CATransformLayer: 0x1700351c0>>
| <EntryItemInfoView: 0x10fd85e50; frame = (66 0; 51 51); autoresize = RM+BM; layer = <CALayer: 0x1702308e0>>
| | <UIStackView: 0x10fd86570; frame = (10 10; 31 31); opaque = NO; autoresize = RM+BM; layer = <CATransformLayer: 0x170230b00>>
| | | <UILabel: 0x10fd86730; frame = (0 0; 31 20.5); text = '45'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x170480f00>>
| | | <UILabel: 0x10fd86c50; frame = (0 20.5; 31 10.5); text = 'grams'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x170299410>>
| <EntryItemInfoView: 0x10fd86ed0; frame = (132 0; 51 51); autoresize = RM+BM; layer = <CALayer: 0x170230ac0>>
| | <UIStackView: 0x10fd62a00; frame = (10 10; 31 31); opaque = NO; autoresize = RM+BM; layer = <CATransformLayer: 0x170230b60>>
| | | <UILabel: 0x10fd870d0; frame = (0 0; 31 20.5); text = '10'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x170481d10>>
| | | <UILabel: 0x10fd87350; frame = (0 20.5; 31 10.5); text = 'units'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x170481090>>
View not found in container hierarchy: <EntryItemInfoView: 0x10fe10840; frame = (0 0; 51 51); autoresize = RM+BM; layer = <CALayer: 0x17422ad20>>
That view's superview: NO SUPERVIEW
答案 0 :(得分:5)
这个功能似乎为我解决了这个问题:
extension UIStackView {
func safelyRemoveArrangedSubviews() {
// Remove all the arranged subviews and save them to an array
let removedSubviews = arrangedSubviews.reduce([]) { (sum, next) -> [UIView] in
self.removeArrangedSubview(next)
return sum + [next]
}
// Deactive all constraints at once
NSLayoutConstraint.deactivate(removedSubviews.flatMap({ $0.constraints }))
// Remove the views from self
removedSubviews.forEach({ $0.removeFromSuperview() })
}
}
答案 1 :(得分:0)
[self.headerStackView.arrangedSubviews each:^(UIView *subview) {
[self.headerStackView removeArrangedSubview:subview]; // <-- Removes Constraints as well
[subview removeConstraints:subview.constraints];// <--- Constraints will be invalid or non existing
[subview removeFromSuperview];
}];
尝试一下:
while let first = stackView.arrangedSubviews.first {
stackView.removeArrangedSubview(first)
first.removeFromSuperview()
}
答案 2 :(得分:0)
我尝试了stackoverflow的许多解决方案,但是没有一个对我有用。当我从堆栈视图中删除所有子视图时,我的应用仍然崩溃。
但是改变stackview的发行版对我有用:
stackView.distribution = .fillProportionally // <- crash
stackView.distribution = .fill // <- work