2014-02-16 135 views
1

我有一個視圖1.導航欄2.UITableView 3.UITextView。當鍵盤出現時將UITextField和UITableView向上移動

當我開始編輯textView時,出現一個鍵盤,我需要爲TextView和TableView設置動畫。我已經實現了:https://stackoverflow.com/a/8704371/1808179,但是將整個視圖動起來,覆蓋導航欄。

我試過單獨動畫TextView的,如:

- (void)keyboardWillShow:(NSNotification*)notification 
{ 
    CGRect chatTextFieldFrame = CGRectMake(chatTextField.frame.origin.x,chatTextField.frame.origin.y-218,chatTextField.frame.size.width,chatTextField.frame.size.height); 
    [UIView animateWithDuration:0.5 animations:^{ chatTextField.frame = chatTextFieldFrame;}]; 
} 

不過,這並不動畫,並且它不會同步動畫旁邊的TableView中。

什麼是最好的方式來動畫的tableView和textView沒有覆蓋導航欄?

+0

您是否嘗試更改表高而非原點? – Wain

+0

@Wain這似乎工作。謝謝! – Spenciefy

回答

6

我通常在解決這個問題時使用下面的代碼片段。

使用UITableView(它只是UIScrollView的一個子類),你應該設置contentInsets,而不是每次只改變一幀。使用半透明鍵盤的iOS7尤其更好。

- (void)viewDidLoad; 
{ 
    [super viewDidLoad]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 
} 

- (void)dealloc; 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 
} 

#pragma mark - Keyboard Notifications 

- (void)keyboardWillShow:(NSNotification *)notification; 
{ 
    NSDictionary *userInfo = [notification userInfo]; 
    NSValue *keyboardBoundsValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; 
    CGFloat keyboardHeight = [keyboardBoundsValue CGRectValue].size.height; 

    CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]; 
    NSInteger animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue]; 
    UIEdgeInsets insets = [[self tableView] contentInset]; 
    [UIView animateWithDuration:duration delay:0. options:animationCurve animations:^{ 
    [[self tableView] setContentInset:UIEdgeInsetsMake(insets.top, insets.left, keyboardHeight, insets.right)]; 
    [[self view] layoutIfNeeded]; 
    } completion:nil]; 
} 

- (void)keyboardWillHide:(NSNotification *)notification; 
{ 
    NSDictionary *userInfo = [notification userInfo]; 
    CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]; 
    NSInteger animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue]; 
    UIEdgeInsets insets = [[self tableView] contentInset]; 
    [UIView animateWithDuration:duration delay:0. options:animationCurve animations:^{ 
    [[self tableView] setContentInset:UIEdgeInsetsMake(insets.top, insets.left, 0., insets.right)]; 
    [[self view] layoutIfNeeded]; 
    } completion:nil]; 
} 
+0

我應該如何處理TextField? – Spenciefy

+0

我認爲在桌面視圖中會自動滾動它,對嗎?如果沒有,你可以抓住它的框架,並在tableView上手動調用'scrollToRect'。 – petehare

+0

textView與tableView分開 - 它位於 – Spenciefy

0

如果有任何想知道,我這是怎麼做的:

- (void)keyboardWillShow:(NSNotification*)notification 
{ 
    CGRect chatTableViewFrame = CGRectMake(0,65,320,chatTableView.frame.size.height-180); 
    [UIView animateWithDuration:0.3 animations:^{ chatTableView.frame = chatTableViewFrame;}]; 

    CGRect chatTextFieldFrame = CGRectMake(chatTextField.frame.origin.x,chatTextField.frame.origin.y-170,chatTextField.frame.size.width,chatTextField.frame.size.height); 
    [UIView animateWithDuration:0.3 animations:^{ chatTextField.frame = chatTextFieldFrame;}]; 


    [self.chatTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.chat.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 
} 

,反之亦然爲keyboardWillHide。

-1

在希望的事件中寫下此行。

self.view.frame = [[UIScreen mainScreen] applicationFrame]; 
相關問題