2012-07-17 128 views
0

我想在鍵盤出現時調整我的UITextView,但我不知道我在做什麼錯誤。設置UIView框架

我出現時的鍵盤這些三行代碼名爲:

CGSize kbSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

textView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-kbSize.height); 

NSLog(@"%f,%f,%f,%f", textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, textView.frame.size.height); 

對於一些很奇怪的原因,我的鍵盤沒有出現,這裏是被記錄:

0.000000, -180.000000,480.000000,180.000000

爲什麼CGRects y原點在0處硬編碼時被設置爲-180?

編輯:textView是我編程創建的UITextView。我沒有使用Interface Builder。

編輯2:看起來問題是視圖旋轉時自動調整大小。

回答

1

你可以試試這個:

- (void) moveTextViewForKeyboard:(NSNotification*)aNotification up:(BOOL)up { 
    NSDictionary* userInfo = [aNotification userInfo]; 
    NSTimeInterval animationDuration; 
    UIViewAnimationCurve animationCurve; 
    CGRect keyboardEndFrame; 

    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve]; 
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration]; 
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame]; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:animationDuration]; 
    [UIView setAnimationCurve:animationCurve]; 

    CGRect newFrame = textView.frame; 
    CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil]; 
    keyboardFrame.size.height -= tabBarController.tabBar.frame.size.height; 
    newFrame.size.height -= keyboardFrame.size.height * (up?1:-1); 
    textView.frame = newFrame; 

    [UIView commitAnimations]; 
} 

希望對大家有所幫助

+0

我已經解決了剛開始一個新的Xcode項目和略超過一點移動代碼這個問題,但你的代碼看起來不錯過,所以有這個upvote! – jadengeller 2012-07-17 06:04:58