2013-02-28 94 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];