- (void)viewWillAppear:(BOOL)animated
{
//啟用監(jiān)聽
[self registerForKeyboardNotifications];
}
- (void)viewWillDisappear:(BOOL)animated
{
//關閉監(jiān)聽
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)registerForKeyboardNotifications
{
//使用NSNotificationCenter 鍵盤出現時
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShown:)
name:UIKeyboardWillChangeFrameNotification object:nil];
//使用NSNotificationCenter 鍵盤隱藏時
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShown:(NSNotification*)aNotification
{
NSDictionary *info = [aNotification userInfo];
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
NSLog(@"高-%f",keyboardSize.height);
NSLog(@"寬-%f",keyboardSize.width);
//輸入框位置動畫加載
[UIView animateWithDuration:duration animations:^{
//將輸入框位置提高
_backgroundScroll.frame = DrawingAdapter(0, -keyboardSize.height, 375, 667);
}];
}
//當鍵盤隱藏的時候
- (void)keyboardWillBeHidden:(NSNotification*)aNotification{
//將輸入框位置還原
_backgroundScroll.frame = DrawingAdapter(0, 0, 375, 667);
}