IOS Keyboard Blocking Uitextfield

Source: Internet
Author: User

two features that iOS often use: Tap the screen and return to hide the virtual keyboard and solve the virtual keyboard blocking Methods of Uitextfield

iOS above the keyboard processing is very impersonal, so these functions need to be implemented by themselves, First, click Return and the screen-hide keyboard

The first to cite the Gemini blog http://my.oschina.net/plumsoft/blog/42545, his article is very good writing. There is great merit in understanding everyone.

In an IOS program, tapping the text box opens the keyboard when you want to enter data in the text box. For IPAD programs. The keyboard has a button that can be used to turn off the keyboard, but the keyboard in the IPhone program does not have this button, but we can take some methods to close it. For example, we can turn off the keyboard by pressing the rerun (sometimes done, the work, etc.) key. Or. More humane, touch the background to turn off the keyboard.

1. First, press the return key to close the keyboard.

When you press the Return key of the keyboard. Produces a did End on Exit event, at this point. We tell the text box to discard the control, so the keyboard disappears.

If we have created a single View application, and open the Viewcontroller.xib file. Dragged up to three Text Field on View. And then. We mapped the three text boxes to ViewController.h, with the names of Firstfield, Secondfield, and Thirdfield in turn.

For example, with:

On this basis, the realization of the light touch Return off the keyboard. The steps are:

(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 that the user is currently interacting with. When the user is using the keyboard. First Responder is the keyboard. Resignfirstresponder method. As the name implies, it is to abandon first Responder.

(3) Let these three text boxes map to the Textfiledreturnediting method. Only the event at this time should be did End on Exit, and the detailed operation is:

Open Assistant Editor. Open Viewcontroller.xib on the left, open ViewController.h on the right, open Connector Inspector on the far right of Xcode, and select the first text box in View. In Connector Inspector, find the Did End on Exit and pull the map line from the circle to the right of it. The Textfiledreturnediting method that maps to ViewController.h. For example, with:

Do the same for the other two text boxes. Now, the keyboard has been turned off with the touch Return key.

2, the following introduction of a more humane approach. Touch the background to turn off the keyboard.

Almost identical to the above steps. First define a method, then implement this method, and then map the specified control to this method. and select a good event to trigger. The difference is that this time we are going to select a control that is not the top text box. Instead, the view itself.

(1) Add the method declaration code in the ViewController.h file:

-(Ibaction) Backgroundtap: (ID) sender;

(2) Implement this method in VIEWCONTROLLER.M:

-(Ibaction) Backgroundtap: (ID) Sender {    [Firstfield resignfirstresponder];    [Secondfield Resignfirstresponder];    [Thirdfield Resignfirstresponder];}

What needs to be said is, [Firstfield Resignfirstresponder]; Suppose Firstfield has firstresponder to abandon it, we don't have to first infer whether Firstfield has it. This statement is completely correct.

(3) Let the view map to this method, just in advance, we have to change the type of view first.

Open Xib, select View, and open Identity Inspector. Select Uicontrol in class:

(4) Open assistant Editor, open viewcontroller.xib on the left, open ViewController.h on the right, open Connector Inspector on the right side of Xcode, Viewcontroller Select Control in the. Xib. Find Touch down in the Connector Inspector and pull the map line from the circle to the right of it. The Backgroundtap method that maps to ViewController.h, for example:

Well, to be able to perform the following look at the effect:

After you open the keyboard, click in the background area and the keyboard will pick up.

Then reviews. On the internet there is also only one backgroundtap function to write. Then the method that all components are resignfirstresponser. The events for the component and the events on the screen point to the same function.

Both of these methods can be used. But then. I'm more inclined to use the same function method. Reason, the reason is involved in the second aspect of knowledge:

How to solve the virtual keyboard blocking Uitextfield

Because the screen is too small for the sake of. A keyboard jumps out to always block the input box, so you need to move the screen to match the keyboard

#pragma mark-

#pragma mark to solve the virtual keyboard blocking Uitextfield method

-(void) Keyboardwillshow: (nsnotification *) Noti

{

// Keyboard input interface adjustment

// height of the keyboard

float height = 216.0;

CGRect frame = self. View. frame;

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

[UIView beginanimations:@ "Curl"context:Nil ]; // Animation starts          

[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, 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, 20.0f, 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 + +-(self. View. frame. size. height -216.0); // Keyboard Height 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-

Just add these three files to your code. And then the self-delegate, such as the upper-right corner of the screen can be implemented to move, but there is often a screen can not be returned after the problem, 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;

} inAdd the code to the BACKGROUNDTAP function so that the screen returns to normal.

IOS Keyboard Blocking Uitextfield

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.