ios - Adding constraint crash down the application -
i have custom uiview designing uitableviewcell, every things work perfect it's not resize properly. , when trying add constraint crashed. used below code.
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"identifier"]; if(!cell) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:@"identifier"]; uiview *vbackview = [[uiview alloc] initwithframe:aframe]; [[cell contentview] addsubview:vbackview]; [vbackview addconstraint:[nslayoutconstraint constraintwithitem:[cell contentview] attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:vbackview attribute:nslayoutattributetop multiplier:1.0 constant:0.0]]; [vbackview addconstraint:[nslayoutconstraint constraintwithitem:[cell contentview] attribute:nslayoutattributeleading relatedby:nslayoutrelationequal toitem:vbackview attribute:nslayoutattributeleading multiplier:1.0 constant:0.0]]; [vbackview addconstraint:[nslayoutconstraint constraintwithitem:[cell contentview] attribute:nslayoutattributebottom relatedby:nslayoutrelationequal toitem:vbackview attribute:nslayoutattributebottom multiplier:1.0 constant:0.0]]; [vbackview addconstraint:[nslayoutconstraint constraintwithitem:[cell contentview] attribute:nslayoutattributetrailing relatedby:nslayoutrelationequal toitem:vbackview attribute:nslayoutattributetrailing multiplier:1.0 constant:0.0]]; } // doing other stuff return cell; }
and in log it's shows
2015-06-13 12:51:57.482 digitalboard[5314:70921] view hierarchy not prepared constraint: <nslayoutconstraint:0x7fb508dc0ae0 uitableviewcell:0x7fb508dbb0a0'cellidentifier'.top == uiview:0x7fb508dc16f0.top> when added view, constraint's items must descendants of view (or view itself). crash if constraint needs resolved before view hierarchy assembled. break on -[uiview _viewhierarchyunpreparedforconstraint:] debug. 2015-06-13 12:51:57.485 digitalboard[5314:70921] view hierarchy unprepared constraint. constraint: <nslayoutconstraint:0x7fb508dc0ae0 uitableviewcell:0x7fb508dbb0a0'cellidentifier'.top == uiview:0x7fb508dc16f0.top> container hierarchy: <uiview: 0x7fb508dc16f0; frame = (0 0; 400 44); autoresize = rm+bm; tag = 1; layer = <calayer: 0x7fb508dc0720>> | <uilabel: 0x7fb508dc5310; frame = (15 0; 105 44); text = 'meeting #'; opaque = no; autoresize = rm+bm; userinteractionenabled = no; tag = 2; layer = <_uilabellayer: 0x7fb508dc54c0>> | <uibutton: 0x7fb508dc5880; frame = (130 7; 80 30); opaque = no; autoresize = rm+bm; tag = 3; layer = <calayer: 0x7fb508da95d0>> | <uibutton: 0x7fb508dc5fa0; frame = (220 7; 80 30); opaque = no; autoresize = rm+bm; tag = 4; layer = <calayer: 0x7fb508dc5c60>> | <uibutton: 0x7fb508dc61c0; frame = (310 7; 80 30); opaque = no; autoresize = rm+bm; tag = 5; layer = <calayer: 0x7fb508dc5e90>> | <uiimageview: 0x7fb508dc6540; frame = (0 42; 400 2); autoresize = rm+bm; userinteractionenabled = no; layer = <calayer: 0x7fb508dc6680>> view not found in container hierarchy: <uitableviewcell: 0x7fb508dbb0a0; frame = (0 0; 320 44); layer = <calayer: 0x7fb508da98e0>> view's superview: no superview 2015-06-13 12:51:57.498 digitalboard[5314:70921] *** terminating app due uncaught exception 'nsgenericexception', reason: 'unable install constraint on view. constraint reference outside subtree of view? that's illegal. constraint:<nslayoutconstraint:0x7fb508dc0ae0 uitableviewcell:0x7fb508dbb0a0'cellidentifier'.top == uiview:0x7fb508dc16f0.top> view:<uiview: 0x7fb508dc16f0; frame = (0 0; 400 44); autoresize = rm+bm; tag = 1; layer = <calayer: 0x7fb508dc0720>>' *** first throw call stack: ( etc....
can understand problem is.
you shouldn't add constraints vbackview
. assume vbackview
subview of cell.contentview
in case should adding constraints cell.contentview
instead.
the crash log tells why:
when added view, constraint's items must descendants of view (or view itself).
change each line of code begin: [cell.contentview addconstraint:...
.
Comments
Post a Comment