If you want to add a button on the keyboard, you can change the button Location Based on the keyboard height in real time. If you do not enter a button, you can click it to hide the keyboard. This method is embodied in many software applications, then I checked some knowledge about the keyboard height detection on the Internet. The following is a Demo. There are many areas to optimize the code for the referers only;
First look at the effect:
First, we have registered two notifications in ViewDidLoada (), [nsicationicationcenterdefacenter], to detect the keyboard dynamics. One is when the keyboard is about to pop up, and the other is the information about the keyboard when the keyboard is about to exit.
-(Void) viewDidLoad
{
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
[Super viewDidLoad];
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (handleKeyboardDidShow :) name: UIKeyboardWillShowNotification object: nil];
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (handleKeyboardWillHide :) name: UIKeyboardWillHideNotification object: nil];
}
One of the six types of keyboard messages can be detected. Function functions can be described based on the literal meaning.
UIKeyboardWillShowNotification: displays the keyboard when the notification is about to be released
UIKeyboardDidShowNotification displays the keyboard immediately after the notification is released
UIKeyboardWillHideNotification
UIKeyboardDidHideNotification
UIKeyboardWillChangeFrameNotification notifies the keyboard of the framework that changes rapidly before release.
The UIKeyboardDidChangeFrameNotification notification immediately changes to the keyboard framework after it is released.
NSLog (@ "% @", NSStringFromSelector (_ cmd); I added it specially. It can display and print the function called by the current program on the console, I added this sentence to each of the following functions. When I perform different operations, I printed the name of the called function, which is suitable for debugging programs;
After registering a Message notification, implement the method for responding to the notification
-(Void) handleKeyboardDidShow :( NSNotification *) notification
{
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
NSDictionary * info = [notification userInfo];
CGRect keyboardFrame;
[[Info objectForKey: UIKeyboardFrameEndUserInfoKey] getValue: & keyboardFrame];
CGSize kbSize = [[info objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue]. size;
CGFloat distanceToMove = kbSize. height;
NSLog (@ "----> dynamic keyboard Height: % f", distanceToMove );
If (exitButton = nil ){
ExitButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
CGRect exitBtFrame = CGRectMake (self. view. frame. size. width-40, self. view. frame. size. height-distanceToMove, 40366f, 30366f );
ExitButton. frame = exitBtFrame;
[ExitButton setImage: [UIImage imageNamed: @ "DoneDown.png"] forState: UIControlStateNormal];
[Self. view addSubview: exitButton];
}
ExitButton. hidden = NO;
[Self adjustPanelsWithKeyBordHeight: distanceToMove];
[ExitButton addTarget: self action: @ selector (CancelBackKeyboard :) forControlEvents: UIControlEventTouchDown];
}
In this function method, it is worth exploring the information contained in the keyboard, because every time the keyboard is popped up, it is also in the form of animation, and its coordinate position and size are included in the userInfo dictionary, now I use
NSLog (@ "--> info: % @", info); prints the info object, which can be stored in Different Storage types. Pay attention to the value method when taking values, I will only mention it here. I hope I will discuss it later,
In this piece of code, five lines are commented later, because the buttons are removed from the view or released when the keyboard is launched, but the application crashes, then there was no release or removal operation.
-(Void) handleKeyboardWillHide :( NSNotification *) notification
{
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
If (exitButton. hidden = NO ){
ExitButton. hidden = YES;
}
// If (exitButton. superview)
//{
// [ExitButton removeFromSuperview];
// [ExitButton release];
//}
}
-(Void) adjustPanelsWithKeyBordHeight :( float) height
{
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
If (exitButton ){
CGRect exitBtFrame = CGRectMake (self. view. frame. size. width-40, self. view. frame. size. height-30, 40f, 30f );
ExitButton. frame = exitBtFrame;
[Self. view addSubview: exitButton];
}
// UIWindow * tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex: 1];
// If (exitButton. superview = nil)
//{
// [TempWindow addSubview: exitButton];
//// Note that this is directly added to the window.
//}
}
-(Void) CancelBackKeyboard :( id) sender
{
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
[TextField resignFirstResponder];
}
-(Void) viewDidUnload
{
[Self setTextField: nil];
ExitButton = nil;
[Super viewDidUnload];
// Release any retained subviews of the main view.
}
-(Void) dealloc {
[TextField release];
[ExitButton release];
[[Nsicationcenter center defacenter center] removeObserver: self]; // remove registered notifications
[Super dealloc];
}