objective c - how to update constraints in ios -


hi new ios , in app have added 1 autogrowingtextview ,

i have added constraints textview 1)top space 2)leading space 3)trailing space 4)height

and have added button on main view , when click button want set textview height "zero"

for have written below code it's not working please me

my code:-

autogrowingtextview:- --------------------- .h file:- ---------      #import <uikit/uikit.h>      @interface auiautogrowingtextview : uitextview      @property (nonatomic) cgfloat maxheight;     @property (nonatomic) cgfloat minheight;      // todo:     //@property(nonatomic) uicontrolcontentverticalalignment verticalalignment;      @end  .m file:- --------- #import "auiautogrowingtextview.h"  @interface auiautogrowingtextview()  @property (strong, nonatomic) nslayoutconstraint *heightconstraint; @property (strong, nonatomic) nslayoutconstraint *maxheightconstraint; @property (strong, nonatomic) nslayoutconstraint *minheightconstraint;  @end  @implementation auiautogrowingtextview  -(id) initwithframe:(cgrect)frame {     self = [super initwithframe:frame];     if (self) {         [self commoninit];     }     return self; }  -(void) awakefromnib {     [self commoninit];     }  -(void) commoninit {     // if using auto layouts, handler height constraint.     (nslayoutconstraint *constraint in self.constraints) {         if (constraint.firstattribute == nslayoutattributeheight) {             self.heightconstraint = constraint;             break;         }     }         if (!self.heightconstraint) {         // todo: might use auto layouts set height of textview using top + bottom constraints. in case want manually create height constraint         } }  - (void) layoutsubviews {     [super layoutsubviews];      if (self.heightconstraint) {         [self handlelayoutwithautolayouts];     }     else {         [self handlelayoutwithoutautolayouts];     }      // center vertically     // we're  supposed have maximum height contstarint in code text view makes intrinsicside higher height of text view - if had enough text.     // code center vertically text view while context size smaller/equal text view frame.     if (self.intrinsiccontentsize.height <= self.bounds.size.height) {         cgfloat topcorrect = (self.bounds.size.height - self.contentsize.height * [self zoomscale])/2.0;         topcorrect = ( topcorrect < 0.0 ? 0.0 : topcorrect );         self.contentoffset = (cgpoint){.x = 0, .y = -topcorrect};     } }  -(void) handlelayoutwithautolayouts {     cgsize intrinsicsize = self.intrinsiccontentsize;     if (self.minheight) {         intrinsicsize.height = max(intrinsicsize.height, self.minheight);     }     if (self.maxheight) {         intrinsicsize.height = min(intrinsicsize.height, self.maxheight);     }     self.heightconstraint.constant = intrinsicsize.height; }  -(void) handlelayoutwithoutautolayouts {     // todo: }  - (cgsize)intrinsiccontentsize {     cgsize intrinsiccontentsize = self.contentsize;      if ([[[uidevice currentdevice] systemversion] floatvalue] >= 7.0) {         intrinsiccontentsize.width += (self.textcontainerinset.left + self.textcontainerinset.right ) / 2.0f;         intrinsiccontentsize.height += (self.textcontainerinset.top + self.textcontainerinset.bottom) / 2.0f;     }      return intrinsiccontentsize; }   @end  mainclass:- ----------     #import "viewcontroller.h" #import "auiautogrowingtextview.h"     @interface viewcontroller ()     @property (nonatomic, weak) iboutlet autogrowingtextview*growingtextview;     @end     @implementation viewcontroller     @synthesize textviewheight;      - (void)viewdidload     {         [super viewdidload];         //self.growingtextview.maxheight = 800;         self.growingtextview.text = @"dispose of resources can recreated.dispose of resources can recreated. dispose of resources can recreated. dispose";      }      - (void)didreceivememorywarning     {         [super didreceivememorywarning];         // dispose of resources can recreated.     }      - (ibaction)buttontapped:(id)sender {         textviewheight.constant = 0;         [self.view setneedslayout];     }     @end 

when change height constraint , call [self.view setneedslayout] layoutsubviews called. inside layoutsubviews again trying change height of textview. before proceeding further can check below condition in layoutsubviews

if (self.heightconstraint.constant == 0) {     return; } 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -