怎么使iOS中的键盘适应高度变化
学习啦在线学习网怎么使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.学习啦在线学习网