2013-02-28 59 views
0

当键盘显示时,我尝试向上移动窗体,我的方法是测试键盘的框架和文本框的帧是否相交。测试键盘在显示时是否隐藏文本字段

- (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)

每个文本字段具有相同的帧值时,这意味着有些事情是错的。那么,如何测试键盘是否隐藏了文本字段,以便我可以上下移动表格。感谢名单。

回答

0

我把整个事情在全屏的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); 
    }]; 

} 
-1

试试这个:

[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {}]; 

并使用信息从[note userInfo];