IOS完美适配键盘高度,解决被遮盖问题的思想

-(void)keyboardFrameChanged :(NSNotification *)notice
{
//    NSDictionary *userInfo  = notice.userInfo;
//    
//    CGRect newFrame =  [userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
//    BOOL isRealChanege = ![NSStringFromCGRect(newFrame) isEqualToString :NSStringFromCGRect(self.oldKeyFrame) ];
//    CGFloat moveY = 0;
//    CGFloat oldY = 0;
//    CGFloat newY = newFrame.origin.y;
//    
//    if([NSStringFromCGRect(CGRectZero) isEqualToString :NSStringFromCGRect(self.oldKeyFrame)])//首次传值
//    {
//        oldY = [userInfo[@"UIKeyboardFrameBeginUserInfoKey"] CGRectValue].origin.y;
//    }else{ //ios键盘传后续传值可能问题,直接采用自己之前保留的
//        oldY = self.oldKeyFrame.origin.y;
//    }
//
//     moveY =  newY - oldY;
//    if (moveY && isRealChanege)
//    {
//        __weak typeof(self) weakSelf = self;
//        [UIView animateWithDuration:[userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue] animations:^{
//            CGRect frame =  weakSelf.frame;
//            weakSelf.frame = CGRectMake(frame.origin.x, frame.origin.y + moveY, frame.size.width, frame.size.height);
//        }];
//        self.oldKeyFrame = newFrame;
//    }
//

1.在键盘第一次出现的时候保存view的旧frame值,

2.并移动到键盘的上面(根据view的高度以及键盘出现的y值计算view的y值,你也可以根据键盘移动的y值来设置contentsize的增长高度,前提是view的父视图的frame等于window的frame,否则自行转换frame

3.当键盘消失的时候,设置oldFrame值。

    if (newFrame.origin.y != ScreenSize.height) //键盘出现
    {
        if([NSStringFromCGRect(CGRectZero) isEqualToString :NSStringFromCGRect(self.oldFrame)])//首次传值
        {
            self.oldFrame = self.frame;
        }
        
        [UIView animateWithDuration:duration animations:^{
            CGRect frame =  weakSelf.frame;
            frame.origin.y =  newFrame.origin.y - weakSelf.frame.size.height;
            weakSelf.frame = frame;
        }];
        
    }else{                                      //键盘隐藏
        
        [UIView animateWithDuration:duration animations:^{
            weakSelf.frame = weakSelf.oldKeyFrame;
        }];
    }


 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChanged:) name:UIKeyboardWillChangeFrameNotification object:nil];



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值