我有一个位于视图底部的搜索栏。
如何设置 UIKeyboard 的位置?
使用 CGAffineTransform 至少从我观察和测试的情况来看,4.0 之前的操作系统版本需要定义转换,从 os 4.0 和更高版本的操作系统开始负责键盘定位。这就是为什么我在设置 Transform 之前检查 systemVersion 的原因。
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0) {
CGAffineTransform translate = CGAffineTransformMakeTranslation(xx.0, yy.0);//use x,y values
[self setTransform:translate];
}
您不能设置键盘的位置,您只能询问键盘它在哪里,并适当地组织您的其他视图。
// 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
}
您没有设置键盘的位置。系统会自动为您完成。
您需要做的是使用键盘通知将搜索栏移动到可见部分