2014-02-16 1 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.

Каков наилучший способ анимации таблицыView и 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

Я думаю, что в представлении таблицы фактически прокручивается автоматически, правда? Если нет, вы можете захватить его и вручную вызвать 'scrollToRect' в tableView. – 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]; 
Смежные вопросы