autolayout - How to disable constraint at run time in ios? -


i having view further having text fields. have set aspect ratio of view self & super view. when click text field keyboard appears resizing view & setting y position keyboard not cover textfields. using below code that.

- (void)textfielddidbeginediting:(uitextfield *)textfield {     /* keyboard visible, move views */     if([textfield isequal:self.txt_twitter] || [textfield isequal:self.txt_linkedin])     {         [uiview animatewithduration:0.1f animations:^          {              cgrect rect = self.view.frame;               // 1. move view's origin text field hidden come above keyboard              // 2. increase size of view area behind keyboard covered up.              rect.origin.y -= 130;             rect.size.height += 130;              self.view.frame = rect;             }];     }  }  - (void)textfielddidendediting:(uitextfield *)textfield {      /* resign first responder, hide keyboard, move views */     /* keyboard visible, move views */     if([textfield isequal:self.txt_twitter] || [textfield isequal:self.txt_linkedin])     {         [uiview animatewithduration:0.1f animations:^          {              cgrect rect = self.view.frame;               // revert normal state.              rect.origin.y += 130;              rect.size.height -= 130;                self.view.frame = rect;            }];      }  } 

now when view height increase it's width increase because have set constraints that. please tell me how can resolve issue. in case don't want increase width of view.

mixing autolayout constraints , direct frame manipulation bad idea in general, should avoid this. suggest make iboutlets height or position constraints of views want move/resize , change constraint.constant in delegate callbacks instead of changing frames.

also, when handling keyboard events it's better listen keyboard notifications (uikeyboardwillshownotification) instead of using text field delegate methods.

added gif illustrate better -

enter image description here

some code project:

- (void)viewdidload {     [super viewdidload];      [self addkeyboardnotificationsobserver];     // additional setup after loading view, typically nib. }  - (void)addkeyboardnotificationsobserver {      [[nsnotificationcenter defaultcenter] addobserver:self                                              selector:@selector(handlekeyboardwillshow:) name:uikeyboardwillshownotification object:nil];     [[nsnotificationcenter defaultcenter] addobserver:self                                              selector:@selector(handlekeyboardwillhide:) name:uikeyboardwillhidenotification object:nil];  }  - (void)handlekeyboardwillshow:(nsnotification *)paramnotification  {      nsdictionary* info = [paramnotification userinfo];      //when switching languages keyboard might change height (emoji keyboard higher keyboards).     //you can both sizes of previous keyboard , new 1 info dictionary.      // size of keyb disappear     __unused cgsize kbsize = [[info objectforkey:uikeyboardframebeginuserinfokey] cgrectvalue].size;      // size of keyb appear     cgsize kbsizenew = [[info objectforkey:uikeyboardframeenduserinfokey] cgrectvalue].size;      //make adjustments constraints here...      self.redsquarebottomconstraint.constant = kbsizenew.height;      //and magic happens!      [self.view layoutifneeded];  }  - (void)handlekeyboardwillhide:(nsnotification *)paramnotification  {     //adjust constraints      self.redsquarebottomconstraint.constant = 0;      [self.view layoutifneeded];  }  - (void)dealloc {      [[nsnotificationcenter defaultcenter] removeobserver:self];  } 

as can see code, extract keyboard's size userinfo dictionary comes notification. pointed out nhgrif, can extract other valuable information it, duration (key - uikeyboardanimationdurationuserinfokey) or curve (key - uikeyboardanimationcurveuserinfokey) of animation when keyboard appears/dissapers. docs here. if want specific animation behaviour can wrap layout call in animation block this:

[uiview animatewithduration:duration                  animations:^{      [self.view layoutifneeded];  }]; 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -