2016-11-29 5 views
1

私はIBでUIStackViewを持っており、viewDidLoadでサブビューを削除して追加しています。サブビューを削除するとクラッシュします。UIStackViewクラッシュからアレンジされたサブビューを削除するApp

[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 
+1

あなたは' [subview.constraintsサブビューremoveConstraints]を削除しようとしたことがありますか? Appleは 'removeArrangedSubview'を呼び出すと位置とサイズの管理を解除し、必要なビューを削除してから' removeFromSuperview'を呼び出すと言います。あなたは制約を気にするべきではありません –

答えて

2

この機能は私のためにそれを修正するようだ:; `:

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() }) 
    } 
} 
関連する問題