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

后端 未结 4 646
甜味超标
甜味超标 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:33

    I seem to remember something like this, though not sure it was the exact same thing. In my case, it was caused by calling addSubview: (or insertSubview:...) with a view which already was a subview. While normally I think UIView deals with that, with auto layout it seems that it's possible for some associated information to be added elsewhere twice, and there can be a crash when trying to clean out that associated information. In my case, the solution was to ensure that I only added subviews once, and this crash (or my similar one anyways) went away.

    0 讨论(0)
  • 2021-01-02 15:37

    In my case this crash happened when I had a view controller in a storyboard but loaded the view contents in from a NIB with the same view controller as the file owner. This normally works fine, but I had the view outlet set in both the storyboard and the NIB, which triggered the iOS6 "add subview twice" bug. By clearing the view outlet in the NIB all was fixed.

    0 讨论(0)
  • 2021-01-02 15:37

    I had / have the same problem.
    The same problem occurs to me, when i present two view controllers modally above each other and want to return to the first one. Disabling autolayout for the first presented view only solves the issue for me.
    In your case self.presentingviewcontroller
    Unfortunately i wasn't able to narrow it down further.

    0 讨论(0)
  • 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 <UIKit/UIKit.h>
    
    @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:<viewToAdd>];
    

    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
    0 讨论(0)
提交回复
热议问题