2

我會調整UITableView的大小並向上滑動UIView,其中包含UITextField這個字段被觸發。這是兩個簡單的原型:當UITextField被觸發時,同步向上滑動UIView並調整UITableView的大小

enter image description here enter image description here

現在我有這樣的代碼:

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDuration:0.3]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    [myView setFrame:CGRectMake(myView.frame.origin.x, myView.frame.origin.y - 167, myView.frame.size.width, myView.frame.size.height)]; // 216 (keyboard's height) - 49 (tabbar's height) = 167 

    [UIView commitAnimations]; 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [textField resignFirstResponder]; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDuration:0.3]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    [myView setFrame:CGRectMake(myView.frame.origin.x, myView.frame.origin.y + 167, myView.frame.size.width, myView.frame.size.height)]; 

    [UIView commitAnimations]; 
    return TRUE; 
} 

的問題是,在鍵盤滑動畫和myView滑動畫是不同步的。如何使這兩個動畫完美同步?

如何在鍵盤可見時調整UITableView的大小,並在鍵盤將隱藏時返回原始高度?

+0

[在成爲FirstFirstResponder或resignFirstResponder的情況下將對象保留在鍵盤上?](http://stackoverflow.com/questions/8704137/keeping-object-on-top-of-keyboard-in-the -event-of-become -firstfirstresponder-or-resign) – 2012-02-05 19:25:29

+0

如何使用UIView作爲tableFooterView在底部顯示。如果現在所有這些都被包裝到UITableViewController中,您可以免費獲得所需的功能。 – Till 2012-02-05 19:55:13

+0

@提示否,因爲我想讓UIView始終可見。 – 2012-02-05 20:35:21

回答

3

簡而言之,您需要訂閱UIKeyboardWillShowNotificationUIKeyboardWillHideNotification。這些通知包含鍵盤動畫的確切參數。

長的答案是https://stackoverflow.com/a/8704371/77567

關於您的標籤欄:我鏈接的答案假設您希望在解除鍵盤關閉時將視圖向下滑動到屏幕的底部邊緣。由於您想將其滑動到標籤欄的邊緣,因此您需要查看鍵盤是隱藏還是顯示(通過檢查note.name)。如果顯示,您應該將視圖的當前幀保存在實例變量中。如果隱藏,則應將視圖的新框架設置爲保存在實例變量中的框架,而不是基於鍵盤的結束框進行設置。

+0

謝謝你的回答。當鍵盤顯示並且字段向上滑動時,該代碼運行良好,但當鍵盤隱藏並且字段滑下時,該代碼不起作用。具體來說,當字段向下滑動時,它在'origin.y = 363'上滑動,並且被UITabBar(49高度)隱藏。 CGRect keyboardFrameForTextField = [self.commentView.superview convertRect:keyboardFrame fromView:nil];'?你知道如何解決tabbar? – 2012-02-05 20:34:10

+0

我修改了我的答案。 – 2012-02-05 21:29:16

+0

謝謝你的男人!偉大的提示! – 2012-02-06 02:29:38