How to keep the input box from being overwritten when keyboard appears on iOS

Source: Internet
Author: User

When the keyboard is requested on the iOS5, the system slides the keyboard up from the bottom of the screen, above the content of the app.

(Inside Wall: http://mikixiyou.iteye.com/blog/1488302)

If you have more content items on your screen, it might overwrite objects such as text input boxes. You must adjust your content so that the input box remains visible.

What are some of the ways you can think about it?

The first kind,

Temporarily adjusts the size of each view in the window so that the keyboard occupies a blank area from the bottom up. The height of the keyboard (keyboard.size.height) is certain to reduce the Y value of all areas in the view to Y-keyboard.size.height.

This method has a limitation, and the method will not work if the sum of all the contents is greater than the window minus the keyboard height.

The second kind,

Embeds all the views in a window into a scrolling view object (Uiscrollview). When the keyboard appears, you scroll the input box to the appropriate position and adjust the content area of the scrolling view.

These operations are implemented through a notification uikeyboarddidshownotification, with the following logical process:

1, according to the dictionary information of the notification UserInfo get the size of the keyboard.

2. Adjust the inset at the bottom of the scrolling view content according to the height value in the size of the keyboard.

3, scroll the target view is the file input box into the view.

The brief code is as follows:

1. Implement two delegate methods for specifying the input box object.

-(void) textfielddidbeginediting: (Uitextfield *) TextField

{

Activefield = TextField;

}

-(void) textfielddidendediting: (Uitextfield *) TextField

{

Activefield = nil;

}

2. Observers for registered notices

-(void) registerforkeyboardnotifications

{

[[Nsnotificationcenter Defaultcenter] Addobserver:self

Selector: @selector (keyboardwasshown:)

Name:uikeyboarddidshownotification Object:nil];

[[Nsnotificationcenter Defaultcenter] Addobserver:self

Selector: @selector (Keyboardwillbehidden:)

Name:uikeyboardwillhidenotification Object:nil];

}

Put this method in the Viewdidappear call.

Also write a removeobserver to put in the viewwilldisappear call.

3, the implementation of the keyboard display notification Selector method

Called when the uikeyboarddidshownotification is sent.

-(void) Keyboardwasshown: (nsnotification*) anotification

{

nsdictionary* info = [Anotification userInfo];

Cgsize kbsize = [[info objectforkey:uikeyboardframebeginuserinfokey] cgrectvalue].size;

Uiedgeinsets contentinsets = uiedgeinsetsmake (0.0, 0.0, kbsize.height, 0.0);

Scrollview.contentinset = contentinsets;

Scrollview.scrollindicatorinsets = contentinsets;

If Active Text field is hidden by keyboard, scroll it s visible

Your application might not need or want this behavior.

CGRect arect = self.view.frame;

ARect.size.height-= Kbsize.height;

if (! Cgrectcontainspoint (Arect, ActiveField.frame.origin)) {

Cgpoint ScrollPoint = cgpointmake (0.0, activefield.frame.origin.y-kbsize.height);

[ScrollView Setcontentoffset:scrollpoint Animated:yes];

}

}

4. How to realize the notification of keyboard disappearance

Called when the uikeyboardwillhidenotification is sent

-(void) Keyboardwillbehidden: (nsnotification*) anotification

{

Uiedgeinsets contentinsets = Uiedgeinsetszero;

Scrollview.contentinset = contentinsets;

Scrollview.scrollindicatorinsets = contentinsets;

}

This method adjusts the value of the inset at the bottom of the content so that the input box is not masked by the keyboard area. It can also be implemented in a different way.

The Third Kind,

Expands the height of the content view, scrolling through the text input box object into the content view.

Add Keyboardwasshown: Override.

-(void) Keyboardwasshown: (nsnotification*) anotification {

    nsdictionary* info = [Anotification userInfo];

    cgsize kbsize = [[Info objectforkey: Uikeyboardframebeginuserinfokey] cgrectvalue].size;

    cgrect bkgndrect = activeField.superview.frame;

    bkgndrect.size.height + = kbsize.height;

    [activefield.superview Setframe:bkgndrect];

    [scrollview Setcontentoffset:cgpointmake (0.0, activefield.frame.origin.y-kbsize.height) Animated:yes];

}

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.