动态监听键盘高度,并进行view的移动(防止遮住textField)

/******************************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中注册监听。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值