IOS development hidden keyboard method summary, ios development hidden keyboard

Source: Internet
Author: User

IOS development hidden keyboard method summary, ios development hidden keyboard

The following are some methods to hide the keyboard that I have summarized.

1. Hide your keyboard

When multiple UITextField controls want to hide their own keypad by clicking "Return", the best way is to use the Did End on Exit event. This event is triggered when you click "Return" in the lower-right corner of the keyboard.
This event has a sender parameter indicating the current text box, so that you can write a common event processing method (. m file)

 
 
  1. -(IBAction) TextField_DidEndOnExit :( id) sender {
  2. // Hide the keyboard.
  3. [Sender resignFirstResponder];
  4. }

Then fill in the declaration of this method in the. h file --

 
 
  1. - (IBAction)TextField_DidEndOnExit:(id)sender; 

Return to the storyboard, and press command + option + enter to open the secondary window, so that the. h file is displayed in the secondary window. Select a UITextField control, right-click the panel, and press and hold the circle next to the Did End on Exit event with the left mouse button, and drag it to the right. the event connection is established on the TextField_DidEndOnExit method of the H file. Then, in the same way, connect the Did End on Exit event of other UITextField controls to the TextField_DidEndOnExit method.

Click "Return" to hide the keyboard of each text box.

2. Click Return to automatically go to the next text box

When there are many text boxes on the page, if you need to click the text box to activate the soft keyboard every time, enter the soft keyboard, click Return to hide the soft keyboard, and then click the next text box ...... This operation is too cumbersome. So we hope to automatically go to the next text box when we click Return. Especially for the last text box, you want to perform the next operation when you click Return.

For example, for the logon page. It contains the account text box (nameTextField), passTextField, and loginButton ).
We hope to jump to the Password text box when you click Return in the account text box, and click Return in the Password text box to execute logon.
Because the functions of the two text boxes are different, you cannot write a TextField_DidEndOnExit as in the previous section for unified processing. Instead, you should create respective event processing methods.

Return to the storyboard, right-click the account text box (nameTextField) to bring up the Panel, hold down the circle next to the Did End on Exit event, and drag it to the right. A dialog box is displayed to name the method. Enter the name (nameTextField_DidEndOnExit) and press enter to confirm. The event method is automatically generated.
In the same way, the connection method of the Did End on Exit event (passTextField_DidEndOnExit) in the passTextField is used ).
Go to the. m file and enter the specific code --

 
 
  1. -(IBAction) nameTextField_DidEndOnExit :( id) sender {
  2. // Move the focus to the next text box.
  3. [Self. passTextField becomeFirstResponder];
  4. }
  5. -(IBAction) passTextField_DidEndOnExit :( id) sender {
  6. // Hide the keyboard.
  7. [Sender resignFirstResponder];
  8. // Click the event to trigger the login button.
  9. [Self. loginButton sendActionsForControlEvents: UIControlEventTouchUpInside];
  10. }

For the account text box to the Password text box, you do not need to hide the keyboard, you just need to call becomeFirstResponder to activate the new text box.
Return the Password text box and execute logon. Because the keyboard is no longer needed, you must call resignFirstResponder to hide the keyboard, and then trigger the UIControlEventTouchUpInside event of the loginButton to log on.

Run it and we can see that the expected results have been achieved. Click Return in the account text box to jump to the Password text box. Click Return in the Password text box to log on.
Why is it "Return"? The conversion text box is different from the login execution function?
Set the Return Key attribute of the account text box to "Next", and set the Return Key attribute of the Password text box to "Done", so that the interface is consistent with the function.

3. Touch the background to hide the keyboard

It is too inflexible to close the keypad only by using Return. You should provide the function of tapping the background to hide the keypad.

On the storyboard, click the background View and set its Custom Class to UIControl to display the Touch Down event.
Right-click the background View pop-up panel, hold Down the circle next to the Touch Down event, and drag it to the blank area of the Right. h file to create a method for handling the event.
Go to the. m file and enter the specific code --

 
 
  1. -(IBAction) View_TouchDown :( id) sender {
  2. // Send resignFirstResponder.
  3. [[UIApplication sharedApplication] sendAction: @ selector (resignFirstResponder) to: nil from: nil forEvent: nil];
  4. }

I would like to share these methods with you to help readers and friends.



How to hide a keyboard in ios

Pull down (not every program can)
 
During IOS development, the keyboard is displayed to block the input box.

First, you must go to the proxy method
-(Void) textViewDidBeginEditing :( UITextView *) textView
.
In this case, you need to move the frame of the entire view up, instead of moving the position of the current text box.
To smooth the movement effect, it is best to place the modified frame action in the animation.
CGRect curFrame = self. view. frame;
[UIView animateWithDuration: 0.3f animations: ^ {
CurFrame. origin. y-= height to be moved up;

Self. view. frame = curFrame;

}];

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.