2016-08-10 2 views
-6

У меня есть UITable с некоторыми пользовательскими ячейками, в последней ячейке у меня есть UITextField, я хочу, чтобы все ячейки прокручивались вверх при открытии клавиатуры (все виды клавиатур, Predictive "включен и отключен).Прокрутите вверх таблицуView при открытии клавиатуры

Я рассмотрел другие вопросы по этой теме и попытался, но он все еще недостаточно хорош (между ячейками есть клавиатура, анимация tableView и клавиатура не синхронизированы, и т.д...).

Не могли бы вы помочь мне в этом?

Спасибо!

+0

проверить это. http://creativecoefficient.net/swift/keyboard-management/ ==> Теоретически только нужно изменить 'scrollView' для вашего' tableview' – jose920405

ответ

2

Взгляните на пример проекта iOS Firechat на Github. Он использует текстовое поле вне таблицыView и перемещает представление вместе с клавиатурой, когда она появляется/исчезает.

В частности в этом разделе:

// Setup keyboard handlers to slide the view containing the table view and 
// text field upwards when the keyboard shows, and downwards when it hides. 
- (void)keyboardWillShow:(NSNotification*)notification 
{ 
    [self moveView:[notification userInfo] up:YES]; 
} 

- (void)keyboardWillHide:(NSNotification*)notification 
{ 
    [self moveView:[notification userInfo] up:NO]; 
} 

- (void)moveView:(NSDictionary*)userInfo up:(BOOL)up 
{ 
    CGRect keyboardEndFrame; 
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] 
    getValue:&keyboardEndFrame]; 

    UIViewAnimationCurve animationCurve; 
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] 
    getValue:&animationCurve]; 

    NSTimeInterval animationDuration; 
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] 
    getValue:&animationDuration]; 

    // Get the correct keyboard size to we slide the right amount. 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:animationDuration]; 
    [UIView setAnimationCurve:animationCurve]; 

    CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil]; 
    int y = keyboardFrame.size.height * (up ? -1 : 1); 
    self.view.frame = CGRectOffset(self.view.frame, 0, y); 

    [UIView commitAnimations]; 
} 

Вот то же самое в Swift, непроверенных:

func moveView(userInfo : NSDictionary, up : Bool){ 

     var keyboardEndFrame : CGRect? 
     userInfo.objectForKey(UIKeyboardFrameEndUserInfoKey)?.getValue(&keyboardEndFrame) 

     var animationCurve : UIViewAnimationCurve? 
     userInfo.objectForKey(UIKeyboardAnimationCurveUserInfoKey)?.getValue(&animationCurve) 

     var animationDuration : NSTimeInterval? 
     userInfo.objectForKey(UIKeyboardAnimationDurationUserInfoKey)?.getValue(&animationDuration) 

     UIView.beginAnimations(nil, context: nil) 
     UIView.setAnimationBeginsFromCurrentState(true) 
     UIView.setAnimationDuration(animationDuration!) 
     UIView.setAnimationCurve(animationCurve!) 

     let keyboardFrame = self.view.convertRect(keyboardEndFrame!, toView: nil) 

     let y = keyboardFrame.size.height * (up ? -1 : 1); 
     self.view.frame = CGRectOffset(self.view.frame, 0, y); 

     UIView.commitAnimations() 
    } 
+1

FS.O5, если вам нужно быстро, поэтому удалите тег object-c в своем вопросе , – jose920405

Смежные вопросы