#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//创建键盘通知
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboardOpen:) name:UIKeyboardWillShowNotification object:nil];
[center addObserver:self selector:@selector(keyboardClose:) name:UIKeyboardWillHideNotification object:nil];
}
-(void)keyboardOpen:(NSNotification *)notification{
//NSLog(@"%@",notification.userInfo);
CGRect keyboardOpendFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect textFrame = self.textField.frame;
textFrame.origin.y = keyboardOpendFrame.origin.y-textFrame.size.height;
NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationOptions options = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue];
[UIView animateWithDuration:duration delay:0 options:options animations:^{
self.textField.frame = textFrame;
} completion:nil];
}
-(void)keyboardClose:(NSNotification *)notification{
NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationOptions options = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue];
CGRect textFrame = self.textField.frame;
textFrame.origin.y = self.view.bounds.size.height-textFrame.size.height;
[UIView animateWithDuration:duration delay:0 options:options animations:^{
self.textField.frame = textFrame;
} completion:nil];
}
- (IBAction)pressReturn:(UITextField *)sender {
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//创建键盘通知
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboardOpen:) name:UIKeyboardWillShowNotification object:nil];
[center addObserver:self selector:@selector(keyboardClose:) name:UIKeyboardWillHideNotification object:nil];
}
-(void)keyboardOpen:(NSNotification *)notification{
//NSLog(@"%@",notification.userInfo);
CGRect keyboardOpendFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect textFrame = self.textField.frame;
textFrame.origin.y = keyboardOpendFrame.origin.y-textFrame.size.height;
NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationOptions options = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue];
[UIView animateWithDuration:duration delay:0 options:options animations:^{
self.textField.frame = textFrame;
} completion:nil];
}
-(void)keyboardClose:(NSNotification *)notification{
NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationOptions options = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue];
CGRect textFrame = self.textField.frame;
textFrame.origin.y = self.view.bounds.size.height-textFrame.size.height;
[UIView animateWithDuration:duration delay:0 options:options animations:^{
self.textField.frame = textFrame;
} completion:nil];
}
- (IBAction)pressReturn:(UITextField *)sender {
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}