- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(action:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(action2:) name:UIKeyboardWillHideNotification object:nil];
}
//点击取消键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.textfield resignFirstResponder];
}
//键盘出现
- (void)action:(NSNotification *)notification{
NSLog(@"%@",notification.userInfo);
CGRect rect = [notification.userInfo[@"UIKeyboardFrameEndUserInfoKey"]CGRectValue];
CGRect currentFrame = self.view.frame;
currentFrame.origin.y -= rect.size.height;
self.view.frame = currentFrame;
}
//键盘消失
- (void)action2:(NSNotification *)notification{
NSLog(@"%@",notification.userInfo);
CGRect rect = [notification.userInfo[@"UIKeyboardFrameBeginUserInfoKey"]CGRectValue];
CGRect currentFrame = self.view.frame;
currentFrame.origin.y += rect.size.height;
self.view.frame = currentFrame;
}