iOS键盘处理

2018-05-21  本文已影响15人  轮子糙

因为这是好久前的文章了重新整理下进行了排版,现在对于键盘处理可以大家可以直接使用IQKeyboardManager ,github地址,IQK的用法我就不说了,直接看github上的使用交了,一句代码就搞定整个项目键盘遮挡问题。

步骤一

- (void)viewWillAppear:(BOOL)animated{
     [superviewWillAppear:animated];
     //接受键盘弹出弹出通知
    [[NSNotification CenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];
    [[NSNotification CenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];
}

步骤二
#pragma mark --处理键盘弹出
- (void)keyboardWillShow:(NSNotification*)noti{
     NSDictionary*dict = noti.userInfo;
     NSLog(@"%@",noti.userInfo);
     NSTimeInterval duration = [dict[UIKeyboardAnimationDurationUserInfoKey]  doubleValue];
          [UIViewanimateWithDuration:duration animations:^{
                self.view.transform=CGAffineTransformMakeTranslation(0, -200);
          }];
}

- (void)keyboardWillHide:(NSNotification*)noti{
     NSDictionary*dict = noti.userInfo;
     NSTimeInterval  duration = [dict[UIKeyboardAnimationDurationUserInfoKey]  doubleValue];
     [UIViewanimateWithDuration:durationanimations:^{
           self.view.transform=CGAffineTransformIdentity;
      }];
}

步骤三
- (void)viewWillDisappear:(BOOL)animated{
         //移除通知
     [[NSNotificationCenter defaultCenter] removeObserver:selfname:UIKeyboardWillShowNotificationobject:nil];
     [[NSNotificationCenter defaultCenter] removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];
}

********************将来的你一定会感激现在拼命的自己,愿每一个努力的人都能有收获!******************************
我的传送门: CSDN github

上一篇 下一篇

猜你喜欢

热点阅读