我尝试在显示键盘时向上移动表单,我的方法是测试键盘的框架和文本字段的框架是否相交。
- (void)keyboardDidShow:(NSNotification *)notification
{
// Get the size of the keyboard.
CGRect keyboardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
//Test whether the current frame of the text field is hidden by the keyboard
if (!CGRectIsNull(CGRectIntersection(keyboardFrame,self.activeField.frame))) {
NSLog(@"Key board frame intersects with the text field frame");
}
}
在上面的代码中,CGRectIsNull
总是返回null。
一个调试语句返回给我这些关于键盘和活动文本字段被选中的信息:
键盘大小=(宽=352,高=1024)键盘原点= (x=-352, y=0)
键盘帧= (-352,0,352,1024)文本框= (200,15,300,30)
每个文本字段都有相同的帧值,这意味着有问题。那么,我如何测试键盘是否隐藏了文本字段,以便上下移动窗体。谢谢。
我会把整个东西放在一个全屏的UIScrollView中,然后在需要的时候调整它的大小…
// This in your init somewhere
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
-(void) keyboardWillChange:(NSNotification*)notify {
CGRect endFrame;
float duration = [[[notify userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
[[[notify userInfo] valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&endFrame];
endFrame = [self.view convertRect:endFrame fromView:nil];
float y = (endFrame.origin.y > self.view.bounds.size.height ? self.view.bounds.size.height : endFrame.origin.y);
[UIView animateWithDuration:duration animations:^{
scrollView.frame = CGRectMake(0, 0, self.view.bounds.size.width, y);
}];
}
试试这个:
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {}];
和使用信息从[note userInfo];