ios - UITextView top margin -
i'm using textview , noticed ios 7 leaves top margin default. see image in following
i read different posts in common solution use:
[textviewtest setcontentinset:uiedgeinsetsmake(<#cgfloat top#>, <#cgfloat left#>, <#cgfloat bottom#>, <#cgfloat right#>)];
but insets custom solution particular device, textview, font size, , on. therefore, there no specific insets applicable solution... worst, have programmatically define different insets account ios devices , orientations.
good news found whenever textview becomes first responder , keyboard shown on screen, top margin disappears after keyboard has gone. way, i'm resizing contentinset on uikeyboarddidshownotification , uikeyboardwillhidenotification.
- see image when keyboard did show:
- see image when keyboard has gone:
is there way simulate keyboard show , hide? content inset disappears explain above.
i have tried making textview become first responder , resign it, approach user have see whole keyboard show-hide animation.
thanks in advance!
my code below:
- (void)viewdidload { [super viewdidload]; } - (void)viewwillappear:(bool)animated { [super viewwillappear:animated]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(handlekeyboarddidshow:) name:uikeyboarddidshownotification object:nil]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(handlekeyboardwillhide:) name:uikeyboardwillhidenotification object:nil]; if(self.topmarginisalreadyresized == no) { [self.mytextview becomefirstresponder]; // keyboard show eliminate top margin when view appears } } - (void)viewwilldisappear:(bool)animated { [super viewwilldisappear:animated]; [[nsnotificationcenter defaultcenter] removeobserver:self]; } - (void)handlekeyboarddidshow:(nsnotification *)notification { if(self.topmarginisalreadyresized == no) { self.topmarginisalreadyresized = yes; // once keyboard has shown when view appears, should hide manually [self.mytextview resignfirstresponder]; } nsvalue *keyboardrectasobject = [[notification userinfo] objectforkey:uikeyboardframeenduserinfokey]; cgrect keyboardrect = cgrectzero; [keyboardrectasobject getvalue:&keyboardrect]; self.mytextview.contentinset = uiedgeinsetsmake(0.0f, 0.0f, keyboardrect.size.height, 0.0f); } - (void)handlekeyboardwillhide:(nsnotification *)notification { self.mytextview.contentinset = uiedgeinsetszero; }
this happens because yor view controller has set the property automaticallyadjustsscrollviewinsets
yes, if set no fine. see question , accepted answer more info.
Comments
Post a Comment