How to ensure that the input box is not overwritten when the keyboard appears on iOS?

Source: Internet
Author: User

How to ensure that the input box is not overwritten when the keyboard appears on iOS?

When you request to display the keyboard on iOS5, the system slides the keyboard from the bottom of the screen, located above the content of the application.

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

 

If there are many content items on the screen, it may overwrite objects such as text input boxes. You must adjust your content to make the input box visible.

What processing methods do you think?

First,

Temporarily adjust the size of each view in the window to make the area occupied by the keyboard from the bottom up blank. The keyboard height (keyboard. size. height) is certain, reducing the y value of the area where all content in the view is located to the y-keyboard.size.height.

This method has a limitation. If the sum of all content is greater than the window minus the keyboard height, this method cannot be used.

Second,

Embed all views in the window into a scroll view object (UIScrollView. When the keyboard appears, you can scroll the input box to the appropriate position and adjust the content area of the scroll view.

These operations are implemented by notifying UIKeyboardDidShowNotification. The logical process is as follows:

1. Obtain the keyboard size Based on the dictionary userInfo of the notification.

2. Adjust the inset at the bottom of the scrolling View content based on the height value in the keyboard size.

3. scroll to the target view, that is, enter the file input box to enter the view.

The simple code is as follows:

1. Implement two delegate methods to specify the input box objects.

-(Void) textFieldDidBeginEditing :( UITextField *) textField

{

ActiveField = textField;

}

 

-(Void) textFieldDidEndEditing :( UITextField *) textField

{

ActiveField = nil;

}

2. register the notification observer

-(Void) registerForKeyboardNotifications

{

[[Nsicationcenter center defacenter center] addObserver: self

Selector: @ selector (keyboardWasShown :)

Name: UIKeyboardDidShowNotification object: nil];

 

[[Nsicationcenter center defacenter center] addObserver: self

Selector: @ selector (keyboardWillBeHidden :)

Name: UIKeyboardWillHideNotification object: nil];

 

}

Call this method in viewDidAppear.

At the same time, you also need to write a removeObserver to call it in viewWillDisappear.

3. Implement the method in selector of the keyboard display notification

 

// 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 so 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 Implement the Notice 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 inset value at the bottom of the content so that the input box is not blocked by the keyboard area. You can also implement it in another way.

 

Third,

Expand the height of the content view and scroll the text input box object to the content view.

Rewrite keyboardWasShown.

 

-(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];

}


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;

}];

Incorrect keyboard buttons are garbled characters in the host computer input box

It is the dust and water stains under your keyboard that cause this phenomenon, that is, power-on. Please clear your keyboard. If it is not possible, just change it.

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.