ios - How can I dynamically set UIView constraints? -
i have custome uitableviewcell. contains labels , uiimageviews top bottom. added constraints labels , uiimageviews. can use systemlayoutsizefittingsize: uilayoutfittingcompressedsize caculate cell height fit content. image in uiimageview loaded online using sdwebimage. @ first can set constraints uiimageview. when image loaded, should change uiimageview's constraint meet size. error occurs. it's ok:
* assertion failure in -[nslayoutconstraint _setsymbolicconstant:constant:], /sourcecache/foundation_sim/foundation-1142.14/layout.subproj/nslayoutconstraint.m:585 2015-04-01 19:57:44.537 hipda[3760:63583]
* terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'constant not finite! that's illegal. constant:nan'
*** first throw call stack:
(
0 corefoundation 0x00000001113cfa75 __exceptionpreprocess + 165
1 libobjc.a.dylib 0x0000000111068bb7 objc_exception_throw + 45
2 corefoundation 0x00000001113cf8da +[nsexception raise:format:arguments:] + 106
3 foundation 0x000000010ef0cb6f -[nsassertionhandler handlefailureinmethod:object:file:linenumber:description:] + 195
4 foundation 0x000000010ee87c7d -[nslayoutconstraint _setsymbolicconstant:constant:] + 149
the code is:
__weak typeof(self) weakself = self; __weak typeof(uiimageview *) weakimgview=imgview; [imgview sd_setimagewithurl:[nsurl urlwithstring:[dic objectforkey:threadlistdetailimage]] completed:^(uiimage *image, nserror *error, sdimagecachetype cachetype, nsurl *imageurl) { cgfloat width=image.size.width>maxwidth?maxwidth:image.size.width; nslayoutconstraint *imgw1=[nslayoutconstraint constraintwithitem:weakimgview attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:nil attribute:nslayoutattributewidth multiplier:0.0 constant:width]; [weakself.contentview addconstraint:imgw1]; nslayoutconstraint *imgh1=[nslayoutconstraint constraintwithitem:weakimgview attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem:nil attribute:nslayoutattributeheight multiplier:0.0 constant:width*image.size.height/image.size.width]; [weakself.contentview addconstraint:imgh1]; [weakself needsupdateconstraints]; [weakself layoutifneeded]; [weakself layoutsubviews]; }];
and if image downloaded completely, tableview doesn't refresh. can see image came out of cell size. how can solve that?
you have take outlet nslayoutconstraint imageview , programmatically can change value like
self.imgviewheightconstraint.constant = 300 (some value)
Comments
Post a Comment