IOS learning notes-keyboard processing, ios learning notes keyboard

Source: Internet
Author: User

IOS learning notes-keyboard processing, ios learning notes keyboard

The information found on the internet is scattered, and it seems a bit empty to learn this part. The content only includes the hidden keyboard and keyboard height.

Hiding the keyboard has actually been practiced in the first program I learned about iOS development, but I was still confused. Now I understand what it is and I will record it, add additional content.

The appearance and hiding of this keyboard are related to the acquisition and loss of the focus in the input box. If the input box gets the focus, the soft keyboard will appear. If the input box loses the focus, the soft keyboard will disappear. This is different from Android. So it's easy to just remove the keyboard with a line of code.

[NameTextField resignFirstResponder]; // nameTextFiled is the name of the input box.

However, the trigger mechanism takes a little more steps. The first thing to make the keyboard disappear is that the text box should disappear after it is input (subject to the Enter key). Then we need to implement the UITextFieldDelegate-(BOOL) textFieldShouldReturn :( UITextField *) textField method, set the delegate in the input box to the current ViewController. The Code is as follows:

@interface HGTabItem2KeyboardViewController : UIViewController<UITextFieldDelegate>

{

}

@end

Code of ViewDidLoad

self.txtTextBox.delegate=self;

Add Method

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

if(self.txtTextBox==textField)

[textField resignFirstResponder];

return true;

} 

When I use a soft keyboard, I always have a habit: I don't want to finish typing once. When I try typing halfway, I don't want to let the soft keyboard disappear, click the blank area and the keyboard disappears. Now that you have learned the touch event, it is not difficult for you to call the resignFirstResponder method in the-(BOOL) textFieldShouldReturn :( UITextField *) textField method, self is also called in touch events. view endEditing. You can use touchesEnded: withEvent: For this touch event, or UITapGestureRecognizer.

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

  [self.view endEditing:true];

}

Declare code

@interface HGTabItem2KeyboardViewController : UIViewController<UITextFieldDelegate,UIGestureRecognizerDelegate>
{

}

@end

Code in 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];

}

Processing the keyboard height actually uses global events of the keyboard. There is a saying on the Internet that after iOS5.0, the keyboard height will change in different input languages, however, after my practice on the simulator, I found that the height difference does not exist, but the keyboard height is still useful. For example, QQ, when the keyboard appears, the entire view will move up, the moving distance is the keyboard height, and the keyboard height is used. The keyboard height is a global event that uses the keyboard.

  • UIKeyboardWillShowNotification;
  • UIKeyboardDidShowNotification;
  • UIKeyboardWillHideNotification;
  • UIKeyboardDidHideNotification;

Two more events are added in iOS5.0.

  • UIKeyboardWillChangeFrameNotification
  • UIKeyboardDidChangeFrameNotification

The above events are called to know when the event is triggered, but the ChangeFrame event is not very effective. When the keyboard height changes, both show and ChangeFrame events are triggered, the following code is stuck:

Add the following method to ViewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidChange:) name:UIKeyboardDidChangeFrameNotification object:nil];

Add the following event Methods

- (void)didReceiveMemoryWarning
{

[super didReceiveMemoryWarning];

// Dispose of any resources that 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 will be 216, on iOS version older than 5.0

CGRect boxFrame = self.view.frame;

boxFrame.origin.y = -[[[notif userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size.height+

self.tabBarController.tabBar.frame.size.height;//50;

[UIView animateWithDuration:0.3f animations:^{

self.view.frame = boxFrame;

}];

NSLog(@"keyboardWillShow");

}

-(void)keyboardWillHide:(NSNotification*)notif
{

CGRect boxFrame = self.view.frame;

boxFrame.origin.y =0; //216;

[UIView animateWithDuration:0.3f animations:^{

self.view.frame = boxFrame;

}];

}

When you click, the text box is moved up, but the text box is at the top of the keyboard, or at the bottom of the view. When the keyboard is hidden, the text box moves down and is at the bottom of the screen.


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.