ios - Show keyboard with UITextField in the input accessory view -


i have buttona , when buttona clicked, want keyboard show uitextfield in inputaccessoryview. there way have keyboard show manually , set inputaccessoryview without having uitextfield initially?

thanks.

you cannot summon keyboard without object can become first responder. there 2 ways work around:

  • subclass uiview , implement uikeyinput protocol in it. example:

    in .h file:

    @interface inputobject : uiview<uikeyinput> @property (nonatomic, copy) nsstring *text; @property (nonatomic, strong) uiview *inputaccessoryview; // must override inputaccessoryview , since it's readonly default @end 

    in .m file, implement protocol:

    - (bool)canbecomefirstresponder {     return yes; }  - (bool)hastext {     return [self.text length]; }  - (void)inserttext:(nsstring *)text {     nsstring *selftext = self.text ? self.text : @"";     self.text = [selftext stringbyappendingstring:text];     [[nsnotificationcenter defaultcenter] postnotificationname:kinputobjecttextdidchangenotification object:self]; }  - (void)deletebackward {     if ([self.text length] > 0)         self.text = [self.text substringtoindex:([self.text length] - 1)];     else         self.text = nil;     [[nsnotificationcenter defaultcenter] postnotificationname:kinputobjecttextdidchangenotification object:self]; } 

let's want summon keyboard in -viewdidappear:, code goes this:

    - (void)viewdidload     {         [super viewdidload];         // inputobject , textfield both ivars in view controller         inputobject = [[inputobject alloc] initwithframe:cgrectmake(0, 0, 100, 100)];         textfield = [[uitextfield alloc] initwithframe:cgrectmake(0, 0, 100, 30)];         inputobject.inputaccessoryview = textfield;         [self.view addsubview:inputobject];         [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(inputobjecttextdidchange:) name:kinputobjecttextdidchangenotification object:nil];      }      - (void)viewdidappear:(bool)animated     {         [super viewdidappear:animated];         [inputobject becomefirstresponder]; // summon keyboard     } 

then implement notification selector in view controller:

    - (void)inputobjecttextdidchange:(nsnotification *)notification     {         // set text here. notification.object inputobject.         textfield.text = ((inputobject *)(notification.object)).text;     } 

this mean "set inputaccessoryview without having uitextfield initially"

  • another workaround let textfield "pretend" inputaccessoryview arranging animation. solution needs textfield first responder.

firstly, observe keyboard events in -viewdidload:

    - (void)viewdidload     {         [super viewdidload];         // init textfield , add subview of self.view         textfield = [[uitextfield alloc] init];         textfield.backgroundcolor = [uicolor redcolor];         [self.view addsubview:textfield];          // register keyboard events         [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwillshownotification:) name:uikeyboardwillshownotification object:nil];         [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwillhidenotification:) name:uikeyboardwillhidenotification object:nil];     } 

secondly, set frame of textfield in -viewwillappear:, in order guarantee frame won't affected autoresizing :

    - (void)viewwillappear:(bool)animated     {         [super viewwillappear:animated];         textfield.frame = cgrectmake(0, cgrectgetmaxy(self.view.bounds), cgrectgetwidth(self.view.bounds), 50);     } 

and then, arrange textfield animation , let synchronized keyboard animation. keyboard notification selectors may this:

    - (void)keyboardwillshownotification:(nsnotification *)notification     {         nsdictionary *userinfo = notification.userinfo;         uiviewanimationcurve curve = [[userinfo valueforkey:uikeyboardanimationcurveuserinfokey] intvalue];         cgfloat duration = [[userinfo valueforkey:uikeyboardanimationdurationuserinfokey] floatvalue];         cgrect keyboardframe = [[userinfo valueforkey:uikeyboardframeenduserinfokey] cgrectvalue];         keyboardframe = [self.view convertrect:keyboardframe toview:self.view];          [uiview animatewithduration:duration delay:0.0f options:(uiviewanimationoptions)curve animations:^{             cgrect textfieldframe = textfield.frame;             textfieldframe.origin.y = keyboardframe.origin.y - cgrectgetheight(textfieldframe);             textfield.frame = textfieldframe;         }completion:nil];     }      - (void)keyboardwillhidenotification:(nsnotification *)notification     {         nsdictionary *userinfo = notification.userinfo;         uiviewanimationcurve curve = [[userinfo valueforkey:uikeyboardanimationcurveuserinfokey] intvalue];         cgfloat duration = [[userinfo valueforkey:uikeyboardanimationdurationuserinfokey] floatvalue];         [uiview animatewithduration:duration delay:0.0f options:(uiviewanimationoptions)curve animations:^{             textfield.frame = cgrectmake(0, cgrectgetmaxy(self.view.bounds), cgrectgetwidth(self.view.bounds), 50);         }completion:nil];     } 

at last, call [textfield becomefirstresponder] trigger animation.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -