views:

103

answers:

3

I have a search bar which is at the bottom of the view.

How can I set the position of the UIKeyboard?

A: 

You cannot set the position of the keyboard, you can only ask the keyboard where it is and organize your other views appropriately.

// Somewhere you need to register for keyboard notifications

- (void)viewDidLoad {
    [super viewDidLoad];

    // Register for keyboard notifications
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(keyboardWasShown:)
                          name:UIKeyboardDidShowNotification object:nil];
    //... do something
}

// At some point you need to unregister for notifications
- (void)viewWillHide {
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter removeObserver:self];
}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    // Caution, this notification can be sent even when the keyboard is already visible
    // You'll want to check for and handle that situation
    NSDictionary* info = [aNotification userInfo];

    NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGSize keyboardSize = [aValue CGRectValue].size;

    //... do something
}
kubi
-1; you also need to un-register for the notifications in dealloc *and* viewDidUnload (otherwise you'll be registered twice when your view is loaded a second time).
tc.
@tc. You are right, but for the wrong reasons. The notification center doesn't retain references to it's observers, so if your class is deallocated, you'll crash when the notification center tries to send it a notification. I'll add the appropriate code.
kubi
My reason was solely regarding viewDidUnload. Last I checked, get double-notifications if you register twice, which may result in odd animations if there's a memory warning.
tc.
+1  A: 

You don't set the position of the keyboard. The system will do it for you automatically.

What you need to do is to move your searchbar to a visible part using keyboard notifications

nacho4d
I have a custom search area. no worries I will move it.
alJaree
`The system will do it for you automatically` in iOS 4.
Emil
I use a UITextField, grab the input and search for any matches myself. Anyway it is solved.
alJaree
+3  A: 

use CGAffineTransform At least from what I have observed and tested that OS version before 4.0 needs to define the transform, from the os 4.0 and greater OS is taking care of keyboard positioning. that's why here I am checking for the systemVersion before setting Transform.

    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0) {
        CGAffineTransform translate = CGAffineTransformMakeTranslation(xx.0, yy.0);//use x,y values
        [self setTransform:translate];
    }
KiranThorat
Thanks but still leaves the other issue.
alJaree
What other issue?
Emil
What issue... ?
KiranThorat
That if the user isn't using 4.0 the keyboard covers the search bar.
alJaree
Thats what the code is for... use proper X,Y values...try different values for x,y and see which gives good results.
KiranThorat