user198725878
user198725878

Reputation: 6386

Keyboard hides the custom cell

I am having a custom cell in my tableview, when showing keyboard some of my cell gets hidden behind the keyboard.

To fix this issue I have tried as below

- (void) textFieldDidBeginEditing:(UITextField *)textField {
    CustomCell *cell = (CustomCell*) [[textField superview] superview];

    NSIndexPath *indexPath = [self.mySmlMsgTemplatesTbl indexPathForCell:cell];
    [self.mySmlMsgTemplatesTbl scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];


   // [self.mySmlMsgTemplatesTbl scrollToRowAtIndexPath:[self.mySmlMsgTemplatesTbl indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

But it seems to be not working.

Upvotes: 1

Views: 622

Answers (4)

Nitin
Nitin

Reputation: 7471

You should try following code as I have shown below. I didn't try it but it should work.

- (void)textFieldDidBeginEditing:(UITextField *)textField{  
    CGPoint point = [self.tableView convertPoint:yourtextview.bounds.origin fromView:yourtextview];
    NSIndexPath* path = [self.tableView indexPathForRowAtPoint:point];
    [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

Upvotes: 1

Paras Joshi
Paras Joshi

Reputation: 20551

i do this type of app you just need the textFieldname or textfield tag ..and you can pgive the tag to textField with visiblecell...

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{

    if(textField.tag==3)
    {
        tableview.frame=CGRectMake(tableview.frame.origin.x, tableview.frame.origin.y-40,tableview.frame.size.width , tableview.frame.size.height+40);

    }
    else if(textField.tag==4)
    {
        tableview.frame=CGRectMake(tableview.frame.origin.x, tableview.frame.origin.y-40,tableview.frame.size.width , tableview.frame.size.height+40);
    }

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(textField.tag==3)
    {
        tableview.frame=CGRectMake(0,0, 320,460);
        //tableview.frame=CGRectMake(tableview.frame.origin.x, tableview.frame.origin.y+70,tableview.frame.size.width , tableview.frame.size.height-70); 
    }
    else if(textField.tag==4)
    {
        tableview.frame=CGRectMake(0,0, 320,460);
        //tableview.frame=CGRectMake(tableview.frame.origin.x, tableview.frame.origin.y+70,tableview.frame.size.width , tableview.frame.size.height-70);    
    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
    if(textField.tag==3)
    {
        tableview.frame=CGRectMake(0,0, 320,460);
        //scrollview.frame=CGRectMake(tableview.frame.origin.x, tableview.frame.origin.y+70,tableview.frame.size.width , tableview.frame.size.height-70);   
    }
    else if(textField.tag==4)
    {
        tableview.frame=CGRectMake(0,0, 320,460);
        //tableview.frame=CGRectMake(scrollview.frame.origin.x, tableview.frame.origin.y-70,tableview.frame.size.width , tableview.frame.size.height+70); 
    }

}

i use here scrollView for Registration form here not a perfect code which you want but i think you can get idea from this code... Hope,this help you.. :)

Upvotes: 0

Warif Akhand Rishi
Warif Akhand Rishi

Reputation: 24248

There is another way. You could try to move the whole view up and down.

Hope this will help

#define     kOFFSET_FOR_KEYBOARD    200.0


- (BOOL)textFieldShouldBeginEditing:(UITextField*)textField {
    [txtField addTarget:self action:@selector(setViewMovedUp:)forControlEvents:UIControlEventEditingDidBegin];
    [txtField addTarget:self action:@selector(setViewMovedDown:)forControlEvents:UIControlEventEditingDidEndOnExit];
}

-(void)setViewMovedUp:(id)sender
{
if (isKeyboardAppeared) {
    return;
}

isKeyboardAppeared = YES;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view

CGRect rect = self.view.frame;

rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;

self.view.frame = rect;
[UIView commitAnimations];
}

-(void)setViewMovedDown:(id)sender
{
[self actionSaveRegistration];
if (!isKeyboardAppeared) {
    return;
}

isKeyboardAppeared = NO;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; 

CGRect rect = self.view.frame;

rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;

self.view.frame = rect;

[UIView commitAnimations];
}

Upvotes: 0

Rengers
Rengers

Reputation: 15238

If it is the last cell for example, this won't work. The tableview can't scroll that far up. You will need to resize the tableview frame or use it's contentInset property to resize it.

Upvotes: 0

Related Questions