0

The standard behaviour when a cell contains a text field and becomes first responder, is automatically set the collection view's content offset to prevent the text field to be hidden.

Why UICollectionView offset changes when keyboard appears

This happens automatically and it's an expected behaviour.

How can I customise the animation of the cell going up, when the keyboard comes up?

Community
  • 1
  • 1
user1447414
  • 1,306
  • 2
  • 12
  • 25

2 Answers2

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

with this u can toggle UICollectionView height accordingly

vaibby
  • 1,255
  • 1
  • 9
  • 23
0

You should implement UIScrollViewDelegate and UITextFieldDelegate and add:

- (void)viewDidLoad 
{    
   [self addNotificationObserver];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self removeObservers];

}


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

- (void)keyboardWillShow:(NSNotification *)notification
{
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    [UIView animateWithDuration:0.1 animations:^{
                    CGRect f = self.view.frame;
                    f.origin.y = -keyboardSize.height;
                    self.view.frame = f;
                }];
  }

-(void)keyboardWillHide:(NSNotification *)notification
{
      [UIView animateWithDuration:0.1 animations:^{
                    CGRect f = self.view.frame;
                    f.origin.y = 0.0f;
                    self.view.frame = f;
                }];
}

-(void)removeObservers 
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIKeyboardWillShowNotification" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIKeyboardWillHideNotification" object:nil];
}
Pek Mario
  • 14
  • 4
  • have you tested this with a UICollectionView and a UITextField in the cell? – user1447414 Dec 15 '15 at 14:23
  • I had a close look to your answer and it sets the view that holds the collectionview. I think this can work yes. But my question is more about how to access the animations details and modify them – user1447414 Dec 15 '15 at 14:41