IOS keyboard blocks UITextField

Source: Internet
Author: User
Tags exit in

IOS keyboard blocks UITextField

Two common functions of iOS: Click screen and return to hide the virtual keyboard and solve the problem of blocking UITextField with the virtual keyboard

The keyboard processing on iOS is very unfriendly, so these functions need to be implemented by yourself. First, click return and hide the keyboard on the screen.

First, we will reference the Gemini blog ghost.

In iOS, if you want to input data in the text box, touch the text box to open the keyboard. For iPad apps, there is a button on the keyboard that can be used to close the keyboard, but the keyboard in the iPhone program does not have such a button, but we can take some ways to close it. For example, you can press the Rerun (sometimes Done or Research) Key to close the keyboard, or, more humane, tap the background to close the keyboard.

1. Press Return to close the keyboard.

When you press the Return key On the keyboard, a Did End On Exit event is generated. In this case, we tell the text box to discard the control, and the keyboard disappears.

Assume that we have created a Single View Application and enabled ViewController. in the xib file, three Text fields are dragged on the View. Then, we map the three Text fields to ViewController. in h, the names are firstField, secondField, and thirdField. For example:

On this basis, you can tap Return to close the keyboard. The steps are as follows:

(1) declare a method in ViewController. h:

- (IBAction)textFiledReturnEditing:(id)sender;

(2) implement this method in ViewController. m:

-(IBAction)textFiledReturnEditing:(id)sender {    [sender resignFirstResponder];}

The so-called First Responder refers to the control with which the user is currently interacting. When a user uses a keyboard, the First Responder is the keyboard. The resignFirstResponder method, as the name suggests, is to discard the First Responder.

(3) map all three text boxes to the textFiledReturnEditing method. However, the event should be Did End On Exit. The specific operation is as follows:

Open the Assistant Editor, and open ViewController on the left. xib. Open ViewController on the right. h. Open Connector Inspector On the rightmost side of Xcode, select the first text box in View, find Did End On Exit in Connector Inspector, and pull the ing line from the circle On the Right of Xcode, maps to ViewController. the textFiledReturnEditing method of h, for example:

Perform the same operation on the other two text boxes. Now, you can touch the Return key to close the keyboard.

2. Next we will introduce a more user-friendly method. Tap the background to close the keyboard. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + release/CwLS9q9a4tqi1xL/release + release/release + v/release/tbQzO2807e9t6jJ + release = "brush: java;">-(IBAction) backgroundTap :( id) sender;

(2) implement this method in ViewController. m:

- (IBAction)backgroundTap:(id)sender {    [firstField resignFirstResponder];    [secondField resignFirstResponder];    [thirdField resignFirstResponder];}

It should be noted that [firstField resignFirstResponder]; indicates that if firstField has FirstResponder, discard it. We do not need to judge whether firstField exists first. This statement is completely correct.

(3) map the View to this method. However, we need to change the View type first.

Open xib, select View, enable Identity Inspector, and select UIControl in class:

(4) Open the Assistant Editor, and open ViewController on the left. xib. Open ViewController on the right. h. Open Connector Inspector at the far right of Xcode and go to ViewController. select Control in xib, find Touch Down in Connector Inspector, and pull the ing line from the circle on the right to map it to ViewController. h's backgroundTap method, such:

Okay, you can run the following command to check the effect:

After opening the keyboard, click in the background area and the keyboard will be folded down.

Then, I will write only one backgroundTap function on the Internet. Then I will resignFirstResponser all the components, that is, point the component event and the screen event to the same function.

Both methods can be used, but I prefer to use the same function. The reason is that the second knowledge is involved:

How to block UITextField with a virtual keyboard

Because the screen is too small, the input box is always blocked when a keyboard jumps out, so you need to move the screen to match the keyboard

# Pragma mark-

# Pragma mark solution for UITextField blocking by virtual keyboard

-(Void) keyboardWillShow :( NSNotification *) noti

{

// Adjust the keyboard input interface

// Keyboard height

Float heights = 216.0;

CGRect frame = self. view. frame;

Frame. size = CGSizeMake (frame. size. width, frame. size. height-height );

[UIView beginAnimations: @ "Curl" context: nil]; // starts the animation.

[UIView setAnimationDuration: 0.30];

[UIView setAnimationDelegate: self];

[Self. view setFrame: frame];

[UIView commitAnimations];

}

-(BOOL) textFieldShouldReturn :( UITextField *) textField

{

// When the user presses return, take focus away from the text field so that the keyboard is dismissed.

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations: @ "ResizeForKeyboard" context: nil];

[UIView setAnimationDuration: animationDuration];

CGRect rect = CGRectMake (0.0f, 0.0f, self. view. frame. size. width, self. view. frame. size. height );

// CGRect rect = CGRectMake (0.0f, 20366f, self. view. frame. size. width, self. view. frame. size. height );

Self. view. frame = rect;

[UIView commitAnimations];

[TextField resignFirstResponder];

Return YES;

}

-(Void) textFieldDidBeginEditing :( UITextField *) textField

{

CGRect frame = textField. frame;

Int offset = frame. origin. y + 32-(self. view. frame. size. height-216.0); // The keyboard height is 216.

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations: @ "ResizeForKeyBoard" context: nil];

[UIView setAnimationDuration: animationDuration];

Float width = self. view. frame. size. width;

Float height = self. view. frame. size. height;

If (offset> 0)

{

CGRect rect = CGRectMake (0.0f,-offset, width, height );

Self. view. frame = rect;

}

[UIView commitAnimations];

}

# Pragma mark-

You only need to add these three files in the code, and then put your own delegate in the upper right corner to move the screen, but there are often problems that cannot be returned after the screen moves, the solution here is

-(IBAction) backgroundTap :( id) sender {

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations: @ "ResizeForKeyboard" context: nil];

[UIView setAnimationDuration: animationDuration];

CGRect rect = CGRectMake (0.0f, 0.0f, self. view. frame. size. width, self. view. frame. size. height );

Self. view. frame = rect;

} Add the code in the backgroundTap function so that the screen will return normal.

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.