Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SetFrame works on iPhone, but not on iPad. Auto resize mask to blame?

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?

like image 891
Moshe Avatar asked Oct 25 '22 00:10

Moshe


1 Answers

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...

like image 169
Zaphod Avatar answered Nov 09 '22 17:11

Zaphod