SetFrame 适用于 iPhone,但不适用于 iPad。自动调整蒙版大小是罪魁祸首?

发布于 2024-12-05 07:17:36 字数 1794 浏览 0 评论 0原文

我正在尝试在键盘显示时调整 UITextView 的大小。在 iPhone 上它运行得非常漂亮。当系统发出键盘通知时,文本视图会调整大小。编辑完成后,我调整它的大小以填充初始空间。 (是的,我假设编辑停止时键盘消失了。我应该更改它。但是,我不认为这是我的问题。)

当我在 iPad 上调整文本视图的大小时,框架会正确调整大小,但应用程序似乎将帧的 Y 值重置为零。这是我的代码:

- (void) keyboardDidShowWithNotification:(NSNotification *)aNotification{

//
//  If the content view being edited
//  then show shrink it to fit above the keyboard.
//

if ([self.contentTextView isFirstResponder]) {

    //
    //  Grab the keyboard size "meta data"
    //

    NSDictionary *info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    //
    //  Calculate the amount of the view that the keyboard hides.
    //
    //  Here we do some confusing math voodoo.
    //
    //  Get the bottom of the screen, subtract that 
    //  from the keyboard height, then take the 
    //  difference and set that as the bottom inset 
    //  of the content text view.
    //

    float screenHeightMinusBottom = self.contentTextView.frame.size.height + self.contentTextView.frame.origin.y;

    float heightOfBottom = self.view.frame.size.height - screenHeightMinusBottom;


    float insetAmount = kbSize.height - heightOfBottom;

    //
    //  Don't stretch the text to reach the keyboard if it's shorter.
    //

    if (insetAmount < 0) {
        return;
    }

    self.keyboardOverlapPortrait = insetAmount;

    float initialOriginX = self.contentTextView.frame.origin.x;
    float initialOriginY = self.contentTextView.frame.origin.y;

    [self.contentTextView setFrame:CGRectMake(initialOriginX, initialOriginY, self.contentTextView.frame.size.width, self.contentTextView.frame.size.height-insetAmount)];


}

为什么这可以在 iPhone 上运行,而不能在 iPad 上运行?另外,我的自动调整大小蒙版是否会发生意外的变化?

I'm trying to resize a UITextView when the keyboard shows. On iPhone it works beautifully. When the the system dispatches a keyboard notification, the text view resizes. When it's done editing, I resize it to fill in the initial space. (Yes, I'm assuming the keyboard is gone when the editing stops. I should change that. However, I don't think that's my issue.)

When I resize the textview on the iPad, the frame resizes correctly, but the app seems to reset the Y value of the frame to zero. Here's my code:

- (void) keyboardDidShowWithNotification:(NSNotification *)aNotification{

//
//  If the content view being edited
//  then show shrink it to fit above the keyboard.
//

if ([self.contentTextView isFirstResponder]) {

    //
    //  Grab the keyboard size "meta data"
    //

    NSDictionary *info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    //
    //  Calculate the amount of the view that the keyboard hides.
    //
    //  Here we do some confusing math voodoo.
    //
    //  Get the bottom of the screen, subtract that 
    //  from the keyboard height, then take the 
    //  difference and set that as the bottom inset 
    //  of the content text view.
    //

    float screenHeightMinusBottom = self.contentTextView.frame.size.height + self.contentTextView.frame.origin.y;

    float heightOfBottom = self.view.frame.size.height - screenHeightMinusBottom;


    float insetAmount = kbSize.height - heightOfBottom;

    //
    //  Don't stretch the text to reach the keyboard if it's shorter.
    //

    if (insetAmount < 0) {
        return;
    }

    self.keyboardOverlapPortrait = insetAmount;

    float initialOriginX = self.contentTextView.frame.origin.x;
    float initialOriginY = self.contentTextView.frame.origin.y;

    [self.contentTextView setFrame:CGRectMake(initialOriginX, initialOriginY, self.contentTextView.frame.size.width, self.contentTextView.frame.size.height-insetAmount)];


}

Why would this work on iPhone, and not work on iPad? Also, can my autoresize masks be making an unexpected change?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯 2024-12-12 07:17:36

就像 @bandejapaisa 所说,我发现方向是一个问题,至少在我的测试期间是这样。

首先,kbSize.height 的使用具有误导性,因为在横向方向上它代表键盘的宽度。因此,当您的代码位于 UIViewController 中时,您可以这样使用它:

float insetAmount = (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)?kbSize.height:kbSize.width) - heightOfBottom;

self.interfaceOrientation 给出了界面的方向(可以与设备方向不同)并且如果给定方向是纵向(顶部或底部),则宏 UIInterfaceOrientationIsPortrait 返回 YES。因此,当界面为纵向时,键盘高度位于 kbSize.height 中,而当界面为横向时,键盘高度位于 kbSize.width 中,因此我们只需测试方向即可以获得良好的价值。

但这还不够,因为我发现 self.view.frame.size.height 值也存在同样的问题。所以我使用了相同的解决方法:

float heightOfBottom = (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)?self.view.frame.size.height:self.view.frame.size.width) - screenHeightMinusBottom;

希望这有帮助......

Like said @bandejapaisa, I found that the orientation was a problem, at least during my tests.

The first thing, is about the use of kbSize.height being misleading, because in Landscape orientation it represents the width of the keyboard. So, as your code is in a UIViewController you can use it this way:

float insetAmount = (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)?kbSize.height:kbSize.width) - heightOfBottom;

The self.interfaceOrientation gives the orientation of the Interface (can be different from the Device orientation) and the macro UIInterfaceOrientationIsPortrait returns YES if the given orientation is Portrait (top or bottom). So as the keyboard height is in the kbSize.height when the interface is Portrait, and in the kbSize.width when the interface is Landscape, we simply need to test the orientation to get the good value.

But that's not enough, cause I've discovered the same problem with the self.view.frame.size.height value. So I used the same workaround:

float heightOfBottom = (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)?self.view.frame.size.height:self.view.frame.size.width) - screenHeightMinusBottom;

Hope this helps...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文