学习啦>学习电脑>电脑硬件知识>键盘鼠标>

怎么使iOS中的键盘适应高度变化

沈迪豪分享

  在ios开发时我们会遇到键盘高度无法适应的问题,这时候该怎么解决呢?下面由学习啦小编教大家怎么解决iOS中的键盘高度变化的问题。
完美解决iOS中的键盘适应高度变化的方法

  #pragma mark - reg & unreg notification

  - (void)regNotification

  {

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

  }

  - (void)unregNotification

  {

  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];

  }

  #pragma mark - notification handler

  - (void)keyboardWillChangeFrame:(NSNotification *)notification

  {

  NSDictionary *info = [notification userInfo];

  CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];

  CGRect beginKeyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];

  CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

  CGFloat yOffset = endKeyboardRect.origin.y - beginKeyboardRect.origin.y;

  CGRect inputFieldRect = self.inputTextField.frame;

  CGRect moreBtnRect = self.moreInputTypeBtn.frame;

  inputFieldRect.origin.y += yOffset;

  moreBtnRect.origin.y += yOffset;

  [UIView animateWithDuration:duration animations:^{

  self.inputTextField.frame = inputFieldRect;

  self.moreInputTypeBtn.frame = moreBtnRect;

  }];

  }

  通过获取键盘消息的开始状态、结束状态,以及变化周期,可以计算出具体的Y偏移,从而在相同时间里做相同偏移量。

猜你喜欢:

1.学习啦在线学习网

2.怎样把电脑上的照片导入iphone

3.iphone照片怎么导入电脑

4.电脑ipad模拟器的安装方法

5.安卓程序员必备的开发工具

6.iPhone5s怎么刷机

    1940038