/******************************3月26日9:26am修订版本***********************************/
1、增加累加高度。防止误差。
/******************************3月26日11:07am修订版本***********************************/
1、将UIKeyboardWillShowNotification通知换成UIKeyboardWillChangeFrameNotification。
主要应用到两个监听:
//UIKeyboardWillShowNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillHideNotification
并且根据通知里的信息获取键盘高度。
思路大致为:
①、通过监听获取键盘高度,并算出键盘Y值。
②、键盘出现时,遍历子控件,获取textField最大的Y值,与键盘Y值相减获得view需要移动的高度,并记录此高度。
③、键盘消失时,移动记录的高度,使view恢复原样。
#import "HTKeyboardParentViewController.h"
static CGFloat const kDurationKeyboard = 0.3;
@interface HTKeyboardParentViewController ()
- (CGFloat)minKeyboardY:(NSNotification *)notification;
- (CGFloat)maxTextFieldY;
- (void)moveViewUpWithHeight:(CGFloat)height;
- (void)moveViewDownWithHeight:(CGFloat)height;
@end
@implementation HTKeyboardParentViewController
{
CGFloat _heightHasMoved;
}
#pragma mark - 生命周期
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = COLOR_BACKGROUND_ENTER;
_heightHasMoved = 0;
}
- (void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewWillAppear:(BOOL)animated
{
[self registerKeyboradNotification];
}
#pragma mark - 点击背景键盘消失
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
#pragma mark - 键盘通知
- (void)registerKeyboradNotification
{
// [[NSNotificationCenter defaultCenter] addObserver:self
// selector:@selector(keyboardWillShow:)
// name:UIKeyboardWillShowNotification
// object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillChangeFrame:)
name:UIKeyboardWillChangeFrameNotification
object:nil];
}
- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
CGFloat maxTextFieldY = [self maxTextFieldY];
CGFloat minKeyboardY = [self minKeyboardY:notification];
if(maxTextFieldY < minKeyboardY){
return;
}
//注意此处由于九宫格和全键盘高度不同,移动的高度需要累加。
_heightHasMoved += maxTextFieldY - minKeyboardY;
[self moveViewUpWithHeight:maxTextFieldY - minKeyboardY];
}
//- (void)keyboardWillShow:(NSNotification *)notification
//{
//
// CGFloat maxTextFieldY = [self maxTextFieldY];
// CGFloat minKeyboardY = [self minKeyboardY:notification];
// if(maxTextFieldY < minKeyboardY){
//
// return;
// }
注意此处由于九宫格和全键盘高度不同,移动的高度需要累加。
// _heightHasMoved += maxTextFieldY - minKeyboardY;
// [self moveViewUpWithHeight:maxTextFieldY - minKeyboardY];
//}
- (void)keyboardWillHide:(NSNotification *)notification
{
[self moveViewDownWithHeight:_heightHasMoved];
_heightHasMoved = 0;
}
#pragma mark - 键盘方法
- (CGFloat)minKeyboardY:(NSNotification *)notification
{
NSDictionary *infoDict = [notification userInfo];
NSValue *value = [infoDict objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardFrame = [value CGRectValue];
return (Screen_Height - keyboardFrame.size.height);
}
- (CGFloat)maxTextFieldY
{
NSArray *subviews = self.view.subviews;
CGFloat maxViewY = self.view.frame.origin.y;
for ( UIView *subview in subviews) {
if([subview isKindOfClass:[UITextField class]]){
CGFloat subviewMaxY = CGRectGetMaxY([self.view convertRect:subview.frame toView:nil]);
if(maxViewY < subviewMaxY){
maxViewY = subviewMaxY;
}
}
}
return maxViewY;
}
- (void)moveViewUpWithHeight:(CGFloat)height
{
[self moveViewWithHeight:height _isMoveUp:YES];
}
- (void)moveViewDownWithHeight:(CGFloat)height
{
[self moveViewWithHeight:height _isMoveUp:NO];
}
- (void)moveViewWithHeight:(CGFloat)moveHeight _isMoveUp:(BOOL)isMoveUp
{
[UIView animateWithDuration:kDurationKeyboard animations:^{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y = viewFrame.origin.y - (isMoveUp?moveHeight:-moveHeight);
self.view.frame = viewFrame;
}];
}
监听通知更普遍的做法是在dealloc中移除监听,在viewDidLoad中注册监听。