Strange Crash when dismissing view controller, auto-layout to blame?

后端 未结 4 645
甜味超标
甜味超标 2021-01-02 15:03

I am experiencing a very strange crash, here is the backtrace.

* thread #1: tid = 0x2403, 0x3379516c CoreFoundation`CFHash + 8, stop reason = EXC_BREAKPOINT          


        
4条回答
  •  离开以前
    2021-01-02 15:47

    Based on idea of Carl Lindberg I prepared and used the following iOS category and I have no crash anymore:

    UIView+AddSubviewWithRemovingFromParent.h

    #import 
    
    @interface UIView (AddSubviewWithRemovingFromParent)
    
    - (void)addSubviewWithRemovingFromParent:(UIView *)view;
    
    @end
    

    UIView+AddSubviewWithRemovingFromParent.m

    #import "UIView+AddSubviewWithRemovingFromParent.h"
    
    @implementation UIView (AddSubviewWithRemovingFromParent)
    
    - (void)addSubviewWithRemovingFromParent:(UIView *)view {
        if (view.superview != nil) {
            [view removeFromSuperview];
        }
        [self addSubview:view];
    }
    
    @end
    

    now you can use the addSubviewWithRemovingFromParent method to add the subView instead of addSubview method like this:

    UITableViewCell *cell = [[UITableViewCell alloc] init];
    [cell.contentView addSubviewWithRemovingFromParent:];
    

    To sum it up:

    1. find all references of addSubView in your controllers where the app is crashing
    2. import the category UIView+AddSubviewWithRemovingFromParent.h
    3. use method addSubviewWithRemovingFromParent instead of addSubView

提交回复
热议问题