I have a search bar which is at the bottom of the view.
How can I set the position of the 开发者_运维百科UIKeyboard?
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];
}
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
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
}
精彩评论