iOS Learning notes-keyboard handling

Source: Internet
Author: User

The information found on the Internet is relatively fragmented, this part of the learning feel a little bit empty, the content will only include the hidden keyboard and keyboard height of two parts

Hide the keyboard actually in my Learning iOS development of the first program has been practiced, but at that time also Mengmengdongdong, now understand what is the same thing, on the record, but also add a bit of content up.

said that the appearance of the keyboard and hide the input box to get and lose focus is related to the input box to get the focus, the soft keyboard will appear, the input box loses focus, the soft keyboard will disappear. This is a discrepancy with Android. So to get the keyboard to disappear is actually a very simple line of code can be completed

// nametextfiled is the name of the input box.

But where it executes, the triggering mechanism adds a little more step. Let the keyboard disappear first of all must be the text box input is complete (in order to press ENTER) should disappear, it is necessary to achieve uitextfielddelegate-(BOOL) Textfieldshouldreturn: (Uitextfield *) The TextField method and sets the delegate of the input box to the current Viewcontroller. The code is as follows

@interface Hgtabitem2keyboardviewcontroller:uiviewcontroller<uitextfielddelegate>{}@end

Viewdidload's Code

Self.txttextbox. delegate=self;

Add method

-(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{if(self.txttextbox==TextField) [ TextField Resignfirstresponder]; return true ;}  

In peacetime use of a soft keyboard always have a habit: not not once typing are want to finish, when typing hit half and then launched do not want to hit the soft keyboard disappear, will click on the screen blank place, the soft keyboard disappears. It is no longer difficult to learn the touch events now, except that in the-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField method, the Resignfirstresponder method is called, Also call Self.view endediting in the event of touch, this touch event can use Touchesended:withevent: or you can use UITapGestureRecognizer

-(void) touchesended: (Nsset *) touches withevent: (uievent *)event{[Self.view endediting: true ];}

Declaring code

@interface Hgtabitem2keyboardviewcontroller:uiviewcontroller<uitextfielddelegate,uigesturerecognizerdelegate>{}  @end

Code in the Viewdidload

UITapGestureRecognizer *gestrue=[[UITapGestureRecognizer alloc]initwithtarget:self Action: @selector (SingleTap :)];gestrue.numberoftouchesrequired=1; [ Self.view Addgesturerecognizer:gestrue];

Add method

-(void) Singletap: (UITapGestureRecognizer *) gecognizer{[self.txttextbox endediting:True ];}

handling the keyboard height is actually taking advantage of the keyboard's global events, and there's a saying on the web iOS5.0 later, the keyboard will be in different input language height will change, but after I practice in the simulator, I found that the height of the difference does not exist, but this keyboard height is still useful, such as QQ , when the keyboard appears, the entire view will move upward, moving the distance is the keyboard height, this is used to the keyboard height. Keyboard height is a global event that uses the keyboard

    • Uikeyboardwillshownotification;
    • Uikeyboarddidshownotification;
    • Uikeyboardwillhidenotification;
    • Uikeyboarddidhidenotification;

in the iOS5.0 two more events have been added

    • Uikeyboardwillchangeframenotification
    • Uikeyboarddidchangeframenotification

The above events look at the name and know when it is triggered, but Changeframe the effect of the event is not obvious, when it is triggered when the keyboard height changes Show and the Changeframe two events, or stick the code down.

in the Viewdidload Add the following methods

Object Objectobject: nil];

Then add the following event method

- (void) Didreceivememorywarning{[super didreceivememorywarning];//Dispose of any resources the can be recreated.} -(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{if(self.txttextbox==TextField) [TextField Resignfirstresponder];return true;} -(void) Keyboardwillchange: (Nsnotification *) Notif{nslog (@"Keyboardwillchange");} -(void) Keyboarddidchange: (nsnotification*) notif{}-(void) Keyboardwillshow: (nsnotification*) Notif
{//keyboard height'll be 216, on IOS version older than 5.0CGRect boxframe=SELF.VIEW.FRAME;BOXFRAME.ORIGIN.Y=-[[[notif userinfo]objectforkey:uikeyboardframeenduserinfokey]cgrectvalue].size.height+Self.tabBarController.tabBar.frame.size.height;//;[UIView animatewithduration:0.3fanimations:^{self.view.frame=Boxframe;}]; NSLog (@"Keyboardwillshow");}-(void) Keyboardwillhide: (nsnotification*) Notif
{CGRect Boxframe=SELF.VIEW.FRAME;BOXFRAME.ORIGIN.Y=0;//216;[UIView animatewithduration:0.3fanimations:^{self.view.frame=Boxframe;}];}

When clicked, the text box moves up, but the text box is just at the top of the keyboard, or at the bottom of the view. When the keyboard is hidden, the text box just moves down and ends up just below the bottom of the screen.

iOS Learning notes-keyboard handling

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.