keyboard size given by NSNotificationCenter
Asked Answered
S

1

7

I want to add a accessoryView on a keyboard called from a UISearchBar. Since UISearchBar does not implement this property, I've just created a toolBar. Following Apple's documentation on the matter, I've decided to use notification center not only to know when the keyboard is called but also to know the size of the keyboard, which changes depending on the orientation.

I've followed the example on the documentation and, on the keyboardWasShown method, I call an animation which will show the toolBar on top of the keyboard. Something like this:

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

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

     NSLog(@"width: %.1f; height: %.1f", keyboardSize.width, keyboardSize.height );

    [self showAccessoryView:keyboardSize.height];
}

and, on the animation I set the frame of the toolbar like this:

 self.auxiliaryKeyboardBar.frame=CGRectMake(0, self.view.frame.size.height-(44+kbh), self.view.frame.size.width, 44);

where 44 is the static height of the toolbar and the kbh is the keyboard.size.heigth passed from the method above.

The problem I'm observing is that the keyboard Size given by the userInfo Dictionary is always refered to the portrait orientation. So, the NSLog on portrait orientation is:

width: 320.0; heigth: 216.0, which is ok

but when I change the orientation to landscape and I call the keyboard, the NSLog is as follows:

width: 162.0; heigth: 480.0, which puts the toolbar out of scope.

so, I ended up adding a conditional before calling the animation, such as this:

if ([self deviceIsPortrait]==YES) {
        [self showAccessoryView:keyboardSize.height];
    }else if ([self deviceIsPortrait]==NO) {
        [self showAccessoryView:keyboardSize.width];
    }

I am now wondering whether or not I'm doing something wrong, because I'm following Apple's example precisely to avoid dependence on the keyboard height (as a float) and I ended up having to add a orientation conditional anyway.

What's going on here?

Septate answered 2/7, 2012 at 17:54 Comment(0)
S
25

I think what you're missing is this:

CGRect keyboardFrameConverted = [mainSubviewOfWindow convertRect:keyboardFrame fromView:window];

That's a bit out of context, so here is the full implementation:

- (void) keyboardDidShow:(NSNotification*)notification {
    CGRect keyboardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSLog(@"keyboard frame raw %@", NSStringFromCGRect(keyboardFrame));

    UIWindow *window = [[[UIApplication sharedApplication] windows]objectAtIndex:0];
    UIView *mainSubviewOfWindow = window.rootViewController.view;
    CGRect keyboardFrameConverted = [mainSubviewOfWindow convertRect:keyboardFrame fromView:window];
    NSLog(@"keyboard frame converted %@", NSStringFromCGRect(keyboardFrameConverted));
}
Sane answered 2/7, 2012 at 18:28 Comment(3)
The CGRect stored under the UIKeyboardFrameBeginUserInfoKey key is in the window's coordinate system (which is always in the non-rotated orientation)Katanga
Thank you both for the answer and the clarification. All together is a bit of a pain in the neck.Septate
This seems to not be working for me on 64-bit devices.Faceoff

© 2022 - 2024 — McMap. All rights reserved.