[英]UITextView resize when keyboard is open calculation
I am trying to resize the UITextView
when the keyboard is open. 打开键盘时,我正在尝试调整UITextView
大小。
in order to give my UITextView
a new size ( so that it doesn't become shadowed by the keyboard) I make the following calculation 为了给我的UITextView
一个新的大小(以便它不会被键盘遮盖),我进行以下计算
firstResult = UITextView bottom coordinate - keyboard top coordinate
firstResult
should now have the size of the shadowed UITextView frame
firstResult
现在应该具有阴影的UITextView frame
的大小
then i do textView.frame.size.height -= firstResult
which now should have a new size that would not be shadowed by the keyboard. 然后我做textView.frame.size.height -= firstResult
现在应该有一个不会被键盘textView.frame.size.height -= firstResult
的新大小。
The problem with the code as it stands out is that there is always part of the UIView that is hidden behind the keyboard. 突出的代码问题在于,UIView的始终始终隐藏在键盘后面。
Could anyone point out what's wrong with my calculations so that the new size is always right? 谁能指出我的计算出了什么问题,以便新的尺寸总是正确的? or any other way that I could use to resize the UITextView appropriately because all examples I find online do not work somehow. 或我可以用来适当调整UITextView大小的任何其他方式,因为我在网上找到的所有示例均无法正常工作。
the code 编码
- (void)keyboardWasShown:(NSNotification *)notification {
CGRect viewFrame = input.frame;
CGFloat textEndCord = CGRectGetMaxY(input.frame);
CGFloat kbStartCord = input.frame.size.height - ([[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]).size.height;
CGFloat result = fabsf( input.frame.size.height - fabsf( textEndCord - kbStartCord ));
viewFrame.size.height -= result;
NSLog(@"Original Height:%f, TextView End Cord: %f, KB Start Cord: %f, resutl: %f, the sum: %f",input.frame.size.height, textEndCord,kbStartCord,result,fabsf( textEndCord - kbStartCord ));
input.frame = viewFrame;
}
There is a problem on the calculation, try this instead, 计算存在问题,请尝试执行此操作,
- (void)keyboardWasShown:(NSNotification *)notification {
CGRect viewFrame = input.frame;
CGFloat textEndCord = CGRectGetMaxY(input.frame);
CGFloat kbStartCord = textEndCord - ([[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]).size.height;
viewFrame.size.height = kbStartCord;
input.frame = viewFrame;
}
Edited 编辑
General formula also for also supporting Landscape mode 通用公式也支持横向模式
- (void)keyboardWasShown:(NSNotification *)notification {
CGFloat keyboardHeight;
CGRect viewFrame = textView.frame;
CGFloat textMaxY = CGRectGetMaxY(textView.frame);
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.width;
} else {
keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;
}
CGFloat maxVisibleY = self.view.bounds.size.height - keyboardHeight;
viewFrame.size.height = viewFrame.size.height - (textMaxY - maxVisibleY);
textView.frame = viewFrame;
}
I had to add the UIInterfaceOrientationIsLandscape
conditional since [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;
我不得不添加UIInterfaceOrientationIsLandscape
条件,因为[[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;
doesn't work when device is on Landscape. 设备在横向上时不起作用。 I know it's a little tricky, the other way of going around this would be detecting device rotation and changing a parameter's value. 我知道这有点棘手,解决该问题的另一种方法是检测设备旋转并更改参数值。 It is up to you. 它是由你决定。
Formula explanation 公式说明
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.