IOS studies the keyboard processing of multiple uitextfields
When using UITextField in IOS development, you often need to consider the handling of the keyboard. Sometimes, the pop-up keyboard will overwrite the UITextField area, affecting user input. In this case, you need to move the view up. In this case, we need to consider two points: 1. The time to modify the view coordinate; 2. How large is the offset of the forward shift. 3. If you do not understand the Section spacing of UITableView, see. The implementation method is as follows: 1,Get the UITextField pointer being editedDefine a global UITextField pointer UITextField * tempTextFiled; In the UITextFieldDelegate proxy method-(Void) textFieldDidBeginEditing :( UITextField *) textFieldModify the value of tempTextFiled to the address of the UITextField being edited. -(Void) textFieldDidBeginEditing :( UITextField *) textField {tempTextFiled = textField ;}
2,Configure the keyboard to handle eventsEnable keyboard listening in-(void) viewDidLoad: [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (keyboardWillShow :) name: UIKeyboardWillShowNotification object: nil]; [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (keyboardWillHide :) name: uikeyboardwillhidtification object: nil]; Use the keyboard display and keyboard hiding methods to obtain the keyboard height in the keyboard display method, configure the keyboard view displacement. [It is worth mentioning that this method will also be executed when the user switches the Chinese and English input methods. Therefore, you do not have to worry about an additional keyboard when switching to the Chinese input method. Points ]. -(Void) keyboardWillShow :( NSNotification *) notification
{
NSDictionary * info = [notification userInfo];
NSValue * avalue = [info objectForKey: UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [self. view convertRect: [avalue CGRectValue] fromView: nil];
Double keyboardHeight = keyboardRect. size. height; // The height of the keyboard.
NSLog (@ "textField superview]. frame. origin. y = % f", [tempTextFiled superview]. frame. origin. y );
NSLog (@ "keyboardHeight = % f", keyboardHeight );
If ([tempTextFiled superview]. frame. origin. y + keyboardHeight + REGISTERTABLE_CELL_HEGHIT) >=( [UIScreen mainScreen] bounds]. size. height-44 ))
{
// At this time, the editing box is covered by the keyboard, and the view is shifted accordingly.
CGRect frame = CGRectMake (0, 44,320, [[UIScreen mainScreen] bounds]. size. height-45 );
Frame. origin. y-= [tempTextFiled superview]. frame. origin. y + keyboardHeight + REGISTERTABLE_CELL_HEGHIT + 20-[UIScreen mainScreen] bounds]. size. height + 44; // offset = Y value of the edit box origin + keyboard height + edit box height-screen height
RegisterTableView. frame = frame;
}
} Then implement keyboard hiding: Implement view reset in UITextFieldDelegate proxy method-(void) textFieldDidEndEditing :( UITextField *) textFieldView or-(void) keyboardWillHide :( NSNotification *) notification method, the following code: CGRect frame = registerTableView. frame; frame. origin. y = 44; // modify the Y coordinate of the view's origin. RegisterTableView. frame = frame;
3,Remove listenerIn the-(void) viewDidDisappear :( BOOL) animated or dealloc method, remove the listener [[nsicationicationcenter defaultCenter] removeObserver: self name: jsonobject: nil]; [nsicationicationcenter defacenter center] removeObserver: self name: UIKeyboardDidHideNotification object: nil]; in this way, no matter how many uitextfields are on our interface, you only need a few simple parts to implement UITextField not covered by the keyboard.