UI keyboard notification and ui keyboard
# Import "ViewController. h"
@ Interface ViewController ()
@ Property (nonatomic, strong) UITextField * tf;
@ End
@ Implementation ViewController
-(Void) viewDidLoad {
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_ Tf = [[UITextField alloc] initWithFrame: CGRectMake (10, self. view. bounds. size. height-40,300, 30)];
_ Tf. borderStyle = UITextBorderStyleRoundedRect;
[Self. view addSubview: _ tf];
/*
Notification center (Singleton)
The notification center is one-to-multiple, that is, the same broadcast can be received by multiple radios.
Proxy is one-to-one
Purpose: 1. It is used to receive and initiate broadcasts.
2. Use the notification name as the channel
*/
/*
Parameter 1: Response class
Parameter 2: response methods in the class
Parameter 3: Notification name (Channel)
Parameter 4: receiving type [note] nil represents any type
*/
//: UIKeyboardWillShowNotification receives the notification to be displayed on the keyboard
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (keyBoardWillShow :) name: UIKeyboardWillShowNotification object: nil];
// UIKeyboardWillHideNotification receives notifications to be hidden on the keyboard
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (keyBoardWillHide :) name: UIKeyboardWillHideNotification object: nil];
}
# Pragma mark-UIKeyboardWillHideNotification
// Hide the keyboard
-(Void) keyBoardWillHide :( NSNotification *) noti
{
// Noti. userInfo is a dictionary. You can output it to see what the dictionary contains.
// NSLog (@ "% @", noti. userInfo );
// Obtain the coordinates after the bullet. [Note that the coordinates are after the bullet.]
CGRect keyBoardEndFrame = [[noti. userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue];
// NSLog (@ "-- % @", NSStringFromCGRect (keyBoardEndFrame ));
[UIView animateWithDuration: 2 animations: ^ {
// Change the position of the text box and let it pop up with the keyboard
CGRect tfRext = _ tf. frame;
TfRext. origin. y = keyBoardEndFrame. origin. y-_ tf. bounds. size. height-10;
_ Tf. frame = tfRext;
}];
}
# Pragma mark-UIKeyBoard notification
// The notification is received, so NSNotification is used for the parameter.
// The keyboard will be displayed
-(Void) keyBoardWillShow :( NSNotification *) noti
{
CGRect keyBoardFrame = [[noti. userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue];
// You can output CGRect using the following methods:
// NSStringFromCGRect convert CGRect into a string to obtain the Frame
// NSLog (@ "% @", NSStringFromCGRect (keyBoardFrame ));
// Obtain the animation duration of the keyboard
// CGFloat keyBoardDuration = [noti. userInfo [UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIView animateWithDuration: 2 animations: ^ {
CGRect tfRect = _ tf. frame;
TfRect. origin. y = keyBoardFrame. origin. y-_ tf. bounds. size. height-10;
_ Tf. frame = tfRect;
}];
}
# Pragma mark-called when your fingers touch any part of the screen
-(Void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event
{
// Hide the keyboard with end editing
[Self. view endEditing: YES];
}
@ End