ios - Objective c - Can't get Auto Layout to work for UIWebView inside UIView -
i have custom uitableview cell , inside cell there many subviews , use auto layout, work fine apart uiwebview, set inside uiview @ h:|-5-[webview] , v:[webview] thing cells dynamically resize , instead of uiwebview sitting in uiview uiwebview sits 5 left , 0 top of cell:
here custom uitableviewcell:
#import "posttableviewyoutubecell.h" @implementation posttableviewyoutubecell @synthesize main; @synthesize top; @synthesize center; @synthesize centerleft; @synthesize centerright; @synthesize bottom; @synthesize labelmessage; @synthesize labelcat; @synthesize labelsubcat; @synthesize labelcountry; @synthesize labeluser; @synthesize labeldate; @synthesize imageviewthreadimage; @synthesize youtube; @synthesize webviewyoutube; @synthesize labelyoutubetitle; nsstring *vid = @""; - (id)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier { self = [super initwithstyle:style reuseidentifier:reuseidentifier]; if (self) { [self.contentview settranslatesautoresizingmaskintoconstraints:no]; self.contentview.autoresizingmask = uiviewautoresizingflexibleheight|uiviewautoresizingflexiblewidth; cgrect screenbound = [[uiscreen mainscreen] bounds]; cgsize screensize = screenbound.size; cgfloat screenwidth = screensize.width; //cgfloat screenheight = screensize.height; main = [uiview new]; [self.contentview addsubview:main]; main.translatesautoresizingmaskintoconstraints = no; [main sizetofit]; main.backgroundcolor = [uicolor whitecolor]; top = [uiview new]; [main addsubview:top]; top.translatesautoresizingmaskintoconstraints = no; [top sizetofit]; top.backgroundcolor = [uicolor whitecolor]; labeluser = [uilabel new]; [top addsubview:labeluser]; labeluser.translatesautoresizingmaskintoconstraints = no; [labeluser sizetofit]; [labeluser setfont:[uifont systemfontofsize:14]]; labeluser.textcolor = [uicolor colorwithred:(114.0/255.0) green:(114.0/255.0) blue:(114.0/255.0) alpha:1.0]; labeldate = [uilabel new]; [top addsubview:labeldate]; labeldate.translatesautoresizingmaskintoconstraints = no; [labeldate sizetofit]; [labeldate setfont:[uifont systemfontofsize:14]]; labeldate.textcolor = [uicolor colorwithred:(114.0/255.0) green:(114.0/255.0) blue:(114.0/255.0) alpha:1.0]; center = [uiview new]; [main addsubview:center]; center.translatesautoresizingmaskintoconstraints = no; [center sizetofit]; center.backgroundcolor = [uicolor whitecolor]; imageviewthreadimage = [uiimageview new]; [center addsubview:imageviewthreadimage]; imageviewthreadimage.translatesautoresizingmaskintoconstraints = no; imageviewthreadimage.backgroundcolor = [uicolor colorwithred:(207.0/255.0) green:(215.0/255.0) blue:(248.0/255.0) alpha:1.0]; [imageviewthreadimage sizetofit]; labelmessage = [uilabel new]; [center addsubview:labelmessage]; labelmessage.translatesautoresizingmaskintoconstraints = no; [labelmessage sizetofit]; [labelmessage setfont:[uifont systemfontofsize:14]]; labelmessage.preferredmaxlayoutwidth = screenwidth - 10 - 36; labelmessage.linebreakmode = nslinebreakbywordwrapping; labelmessage.numberoflines = 0; labelmessage.textcolor = [uicolor lightgraycolor]; //labelmessage.backgroundcolor = [uicolor colorwithred:(207.0/255.0) green:(215.0/255.0) blue:(248.0/255.0) alpha:1.0]; //labelmessage.linebreakmode = uilinebreakmodewordwrap; bottom = [uiview new]; [main addsubview:bottom]; bottom.translatesautoresizingmaskintoconstraints = no; [bottom sizetofit]; labelcat = [uilabel new]; [bottom addsubview:labelcat]; labelcat.translatesautoresizingmaskintoconstraints = no; [labelcat sizetofit]; [labelcat setfont:[uifont systemfontofsize:12]]; labelcat.textcolor = [uicolor colorwithred:(58.0/255.0) green:(82.0/255.0) blue:(207.0/255.0) alpha:1.0]; labelcat.linebreakmode = nslinebreakbywordwrapping; labelcat.numberoflines = 0; labelsubcat = [uilabel new]; [bottom addsubview:labelsubcat]; labelsubcat.translatesautoresizingmaskintoconstraints = no; [labelsubcat sizetofit]; [labelsubcat setfont:[uifont systemfontofsize:12]]; labelsubcat.textcolor = [uicolor colorwithred:(58.0/255.0) green:(82.0/255.0) blue:(207.0/255.0) alpha:1.0]; labelcountry = [uilabel new]; [bottom addsubview:labelcountry]; labelcountry.translatesautoresizingmaskintoconstraints = no; [labelcountry sizetofit]; [labelcountry setfont:[uifont systemfontofsize:12]]; labelcountry.textcolor = [uicolor colorwithred:(58.0/255.0) green:(82.0/255.0) blue:(207.0/255.0) alpha:1.0]; youtube = [uiview new]; [main addsubview:youtube]; youtube.translatesautoresizingmaskintoconstraints = no; [youtube sizetofit]; webviewyoutube = [uiwebview new]; [youtube addsubview:webviewyoutube]; webviewyoutube.translatesautoresizingmaskintoconstraints = no; //[webviewyoutube sizetofit]; [webviewyoutube setallowsinlinemediaplayback:yes]; [webviewyoutube setmediaplaybackrequiresuseraction:no]; youtube.backgroundcolor = [uicolor colorwithred:(58.0/255.0) green:(82.0/255.0) blue:(207.0/255.0) alpha:1.0]; } return self; } - (void)layoutsubviews { [super layoutsubviews]; // make sure contentview layout pass here subviews have frames set, // need use set preferredmaxlayoutwidth below. [self.contentview setneedslayout]; [self.contentview layoutifneeded]; // set preferredmaxlayoutwidth of mutli-line bodylabel based on evaluated width of label's frame, // allow text wrap correctly, , result allow label take on correct height. self.labelmessage.preferredmaxlayoutwidth = cgrectgetwidth(self.labelmessage.frame); } - (void)updateconstraints { [super updateconstraints]; if (self.didsetupconstraints) return; nsdictionary *viewsdictionary7 = @{@"main":main}; nsarray *constraint_h37 = [nslayoutconstraint constraintswithvisualformat:@"v:|[main]|" options:0 metrics:nil views:viewsdictionary7]; nsarray *constraint_v37 = [nslayoutconstraint constraintswithvisualformat:@"h:|[main]|" options:0 metrics:nil views:viewsdictionary7]; [self.contentview addconstraints:constraint_h37]; [self.contentview addconstraints:constraint_v37]; //nsstring *text = labelmessage.text; nsdictionary *viewsdictionary3 = @{@"top":top,@"center":center,@"bottom":bottom,@"youtube":youtube}; nsarray *constraint_h3 = [nslayoutconstraint constraintswithvisualformat:@"v:|-10-[top]-5-[center(>=50)]-5-[bottom]-5-[youtube(100)]-10-|" options:0 metrics:nil views:viewsdictionary3]; nsarray *constraint_h33cvcv4545 = [nslayoutconstraint constraintswithvisualformat:@"h:|[youtube]|" options:0 metrics:nil views:viewsdictionary3]; nsarray *constraint_h33 = [nslayoutconstraint constraintswithvisualformat:@"h:|[top]|" options:0 metrics:nil views:viewsdictionary3]; nsarray *constraint_h333 = [nslayoutconstraint constraintswithvisualformat:@"h:|[center]|" options:0 metrics:nil views:viewsdictionary3]; nsarray *constraint_h3335657 = [nslayoutconstraint constraintswithvisualformat:@"h:|[bottom]|" options:0 metrics:nil views:viewsdictionary3]; nsdictionary *viewsdictionary4 = @{@"labeluser":labeluser,@"labeldate":labeldate}; nsarray *constraint_h4 = [nslayoutconstraint constraintswithvisualformat:@"v:|[labeluser]|" options:0 metrics:nil views:viewsdictionary4]; nsdictionary *viewsdictionary45 = @{@"labeldate":labeldate}; nsarray *constraint_h4555 = [nslayoutconstraint constraintswithvisualformat:@"v:|[labeldate]|" options:0 metrics:nil views:viewsdictionary45]; nsarray *constraint_h44 = [nslayoutconstraint constraintswithvisualformat:@"h:|-5-[labeluser]-20-[labeldate]-5-|" options:0 metrics:nil views:viewsdictionary4]; nsdictionary *viewsdictionary48 = @{@"labelmessage":labelmessage,@"imageviewthreadimage":imageviewthreadimage}; nsarray *constraint_h48 = [nslayoutconstraint constraintswithvisualformat:@"v:|-5-[labelmessage]-5-|" options:0 metrics:nil views:viewsdictionary48]; nsarray *constraint_h48342 = [nslayoutconstraint constraintswithvisualformat:@"v:|-8-[imageviewthreadimage(36)]" options:0 metrics:nil views:viewsdictionary48]; nsarray *constraint_h448345fgdfg = [nslayoutconstraint constraintswithvisualformat:@"h:|-5-[imageviewthreadimage(36)]" options:0 metrics:nil views:viewsdictionary48]; cgrect screenbound = [[uiscreen mainscreen] bounds]; cgsize screensize = screenbound.size; cgfloat screenwidth = screensize.width; //cgfloat screenheight = screensize.height; nsarray *constraint_h448345fgdfgdfdf454 = [nslayoutconstraint constraintswithvisualformat:[nsstring stringwithformat:@"h:|-46-[labelmessage(%f)]-10-|",screenwidth - 51] options:0 metrics:nil views:viewsdictionary48]; /**nsarray *constraint_h448 = [nslayoutconstraint constraintswithvisualformat:@"h:|-5-[labelmessage]-5-|" options:0 metrics:nil views:viewsdictionary48];**/ nsdictionary *viewsdictionary488 = @{@"labelcat":labelcat,@"labelcountry":labelcountry,@"labelsubcat":labelsubcat}; nsarray *constraint_h488 = [nslayoutconstraint constraintswithvisualformat:@"v:|-5-[labelcat]|" options:0 metrics:nil views:viewsdictionary488]; nsarray *constraint_h48898 = [nslayoutconstraint constraintswithvisualformat:@"v:|-5-[labelcountry]|" options:0 metrics:nil views:viewsdictionary488]; nsarray *constraint_h48898fgf54 = [nslayoutconstraint constraintswithvisualformat:@"v:|-5-[labelsubcat]|" options:0 metrics:nil views:viewsdictionary488]; nsarray *constraint_h4488 = [nslayoutconstraint constraintswithvisualformat:@"h:|-5-[labelcat]-10-[labelsubcat]-10-[labelcountry]" options:0 metrics:nil views:viewsdictionary488]; nsdictionary *viewsdictionary8 = @{@"webviewyoutube":webviewyoutube}; nsarray *constraint_h378 = [nslayoutconstraint constraintswithvisualformat:@"v:[webviewyoutube(80)]" options:0 metrics:nil views:viewsdictionary8]; nsarray *constraint_v378 = [nslayoutconstraint constraintswithvisualformat:@"h:|-5-[webviewyoutube(100)]" options:0 metrics:nil views:viewsdictionary8]; [youtube addconstraints:constraint_h378]; [youtube addconstraints:constraint_v378]; [self.contentview addconstraints:constraint_h3]; [self.contentview addconstraints:constraint_h33]; [self.contentview addconstraints:constraint_h333]; [self.contentview addconstraints:constraint_h4]; [self.contentview addconstraints:constraint_h44]; [self.contentview addconstraints:constraint_h48]; //[self.contentview addconstraints:constraint_h448]; [self.contentview addconstraints:constraint_h4555]; [self.contentview addconstraints:constraint_h3335657]; [self.contentview addconstraints:constraint_h488]; [self.contentview addconstraints:constraint_h48342]; [self.contentview addconstraints:constraint_h4488]; [self.contentview addconstraints:constraint_h448345fgdfg]; [self.contentview addconstraints:constraint_h48898]; [self.contentview addconstraints:constraint_h48898fgf54]; [self.contentview addconstraints:constraint_h448345fgdfgdfdf454]; [self.contentview addconstraints:constraint_h33cvcv4545]; self.didsetupconstraints = yes; } -(void)setyoutube: (nsstring*)youtubeid :(nsstring*)title { dispatch_async( dispatch_get_main_queue(), ^{ nsstring* embedhtml = [nsstring stringwithformat:@"\ <html>\ <body style='margin:0px;padding:0px;'>\ <script type='text/javascript'>\ </script>\ <iframe id='playerid' type='text/html' width='%d' height='%d' src='http://www.youtube.com/embed/%@?enablejsapi=1&rel=0&playsinline=1' frameborder='0'>\ </body>\ </html>", 100, 80, youtubeid]; [[nsurlcache sharedurlcache] removeallcachedresponses]; [webviewyoutube loadhtmlstring:embedhtml baseurl:[[nsbundle mainbundle] resourceurl]]; }); } - (void)awakefromnib { // initialization code } - (void)setselected:(bool)selected animated:(bool)animated { [super setselected:selected animated:animated]; // configure view selected state } @end
Comments
Post a Comment