键盘打开计算时,UITextView调整大小 [英] UITextView resize when keyboard is open calculation

查看:51
本文介绍了键盘打开计算时,UITextView调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在键盘打开时调整 UITextView 的大小.

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

firstResult = UITextView bottom coordinate - keyboard top coordinate

firstResult 现在应该具有阴影的 UITextView框架

firstResult should now have the size of the shadowed UITextView frame

然后我执行 textView.frame.size.height-= firstResult ,现在应该具有不会被键盘遮盖的新大小.

then i do textView.frame.size.height -= firstResult which now should have a new size that would not be shadowed by the keyboard.

突出显示代码的问题是,UIView的始终有部分隐藏在键盘后面.

The problem with the code as it stands out is that there is always part of the UIView that is hidden behind the keyboard.

有人可以指出我的计算出了什么问题,以便新的尺寸始终正确吗?或我可以用来适当调整UITextView大小的任何其他方式,因为我在网上找到的所有示例均无法正常工作.

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.

代码

- (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;
    }

已编辑

也支持横向模式的通用公式

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;
}

我必须添加 UIInterfaceOrientationIsLandscape 有条件,因为 [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue] .size.height; 在打开设备时不起作用风景.我知道这有点棘手,解决此问题的另一种方法是检测设备旋转并更改参数值.由你决定.

I had to add the UIInterfaceOrientationIsLandscape conditional since [[[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.

公式说明

这篇关于键盘打开计算时,UITextView调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆