Wednesday, September 25, 2013

Avoid Keyboard in UITableView Cell

The best solution to avoid keyboard in iOS in table view are to use framework called TPKeyboardAvoid https://github.com/michaeltyson/TPKeyboardAvoiding

But, if you need some manual solution here are few codes

-(void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

}

-(void) viewDidDisappear:(BOOL)animated  {
    [super viewDidDisappear:YES];
    blogParser.delegate = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

-(CGFloat)getLabelHeightForIndex:(NSInteger)index
{
    CGSize maximumSize = CGSizeMake(230, 10000);
    Blog_Comment *comment = comments[index];
    
    CGSize labelHeighSize = [comment.comment sizeWithFont: [UIFont fontWithName:@"Helvetica" size:12.0f]
                                     constrainedToSize:maximumSize
                                         lineBreakMode:NSLineBreakByCharWrapping];
    return labelHeighSize.height;
    
}

#pragma mark - Notification 

-(void) moveTableViewBottomRowUp:(NSNotification *)note {
    NSDictionary *userInfo = [note userInfo];
    CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    kKeyboardHeight = kbSize.height;
    
    NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        UIEdgeInsets edgeInsets = UIEdgeInsetsMake(0, 0, kbSize.height, 0);
        [_tableView setContentInset:edgeInsets];
        [_tableView setScrollIndicatorInsets:edgeInsets];
//        [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[_tableView numberOfRowsInSection:0]-1 inSection:0]
//                          atScrollPosition:UITableViewScrollPositionTop
//                                  animated:YES];
    }];
}

-(void) moveTableViewBottom2Normal:(NSNotification *)note {
    NSDictionary *userInfo = [note userInfo];
    NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    [UIView animateWithDuration:duration animations:^{
        UIEdgeInsets edgeInsets = UIEdgeInsetsZero;
        [_tableView setContentInset:edgeInsets];
        [_tableView setScrollIndicatorInsets:edgeInsets];
    }];
}

-(void) assignKeyBoardHeigh:(NSNotification *) note {
    NSDictionary *userInfo = [note userInfo];
//    NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    kKeyboardHeight = kbSize.height;
}

-(void) keyboardWillShow:(NSNotification *)note {
    [self assignKeyBoardHeigh:note];
    [self moveTableViewBottomRowUp:note];
    [self moveTextFieldUp];
}

-(void) keyboardWillHide:(NSNotification *)note {
//    [self moveTableViewBottom2Normal:note];
    [self moveTexyFieldDown];
}


-(void) moveTextFieldUp {
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:0.2];
    CGRect frame = viewComment.frame;
    frame.origin.y = self.view.bounds.size.height - kKeyboardHeight - frame.size.height;
    viewComment.frame = frame;
    // DLog(@"%f", viewComment.frame.origin.y);
    [UIView commitAnimations];
}

-(void) moveTexyFieldDown {
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:0.2];
    CGRect frame = viewComment.frame;
    frame.origin.y = self.view.bounds.size.height - frame.size.height;
    viewComment.frame = frame;
    
    // DLog(@"%f", viewComment.frame.origin.y);
    [UIView commitAnimations];
}



http://iphoneimei.info/

1 comment:

  1. Congratulation for the great post. Those who come to read your Information will find lots of helpful and informative tips. Apple Enterprise Account

    ReplyDelete