iOS 计算键盘输入框

用KOV(键值监听就能实现,监听键盘弹出)然后做出计算,在网上也有类似第三方,在touchesBegan(点击空白处)方法调用监听


//点击隐藏键盘
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{
[self.comview resignFirstResponder];
[self MyNotifications];
}

//注册通知监听键盘是否弹出
- (void)MyNotifications
{
//使用NSNotificationCenter 键盘出现时
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(KeyboardPopsUp:)
name:UIKeyboardWillChangeFrameNotification object:nil];
//使用NSNotificationCenter 键盘隐藏时
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(Keyboardishidden:)
name:UIKeyboardWillHideNotification object:nil];
}

//键盘弹出方法
- (void)KeyboardPopsUp:(NSNotification*)aNotification
{
//    计算键盘高度
NSDictionary *info = [aNotification userInfo];
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
//输入框位置动画加载
[UIView animateWithDuration:duration animations:^{
self.comview.hidden = NO;
self.comview是输入框,我设定输入框高度是60,就能准确的计算键盘输入框的高度;
self.comview.frame = CGRectMake(0, self.view.frame.size.height-(keyboardSize.height+60),self.view.frame.size.width, 60);
}];
}

//当键盘隐藏的时候键盘隐藏的时候将输入框从视图上移除就可以了
- (void)Keyboardishidden:(NSNotification*)aNotification{
[self.comview removeFromSuperview];
}

//键盘发送按钮这个方法是将键盘的完成按钮换成发送
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[self.chview ReceiveTheValue:self.comview.text];
[self.comview resignFirstResponder];
return YES;
}

能力有限,只能写这么多.有什么问题欢迎大家留言指正,我看到一定回复.感谢!

你可能感兴趣的:(iOS 计算键盘输入框)